본문 바로가기

백준/실버

[백준 11004번] K번째 수 (C++)

문제링크 : https://www.acmicpc.net/problem/11004

 

11004번: K번째 수

수 N개 A1, A2, ..., AN이 주어진다. A를 오름차순 정렬했을 때, 앞에서부터 K번째 있는 수를 구하는 프로그램을 작성하시오.

www.acmicpc.net

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAX = 987654321;

int N, K, arr[5000001];

int main()
{
    ios_base::sync_with_stdio(0); 
	cin.tie(0);

    cin >> N >> K;
    for(int i=1; i<=N; i++) cin >> arr[i];
    sort(arr+1, arr+1+N);
    cout << arr[K];
    
    return 0;
}

 

간단한 정렬문제이다.

입력받은 배열을 정렬후 K 번째에 해당하는 수를 출력해주면 된다.

주의해야 할 점으로는 i=1부터 배열을 입력받았을 때 정렬도 마찬가지로 arr+1을 해주어야한다.