Algorithm/BOJ
[C/C++] 백준 11004번 - k번째 수
poopooreum
2023. 9. 13. 19:52
반응형
백준 11004번 K번째 수
https://www.acmicpc.net/problem/11004
11004번: K번째 수
수 N개 A1, A2, ..., AN이 주어진다. A를 오름차순 정렬했을 때, 앞에서부터 K번째 있는 수를 구하는 프로그램을 작성하시오.
www.acmicpc.net
기본적인 sort함수를 이용해서 정렬하였고 정렬할 때 cmp함수 안의 값을
if (a < b) return true; else return false; 로 바꿔주었습니다.
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool cmp(int a, int b) {
if (a < b)
return true;
else
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,k;
cin >> n >> k;
vector<int>arr(n);
for (int x = 0; x < n; x++)
cin >> arr[x];
sort(arr.begin(), arr.end(),cmp);
cout << arr[k - 1];
}
기본적인 sort함수를 이용해서 정렬하였고 정렬할 때 cmp함수 안의 값을
if (a < b)
return true;
else
return false;
로 바꿔주었습니다.
반응형