티스토리 뷰

백준

백준 소스코드 [C++] 11004 K번째 수

Hani_Levenshtein 2020. 9. 9. 15:25

www.acmicpc.net/problem/11004

 

11004번: K번째 수

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

www.acmicpc.net

백준 소스코드 [C++] 11004 K번째 수

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n, m,p;
	cin >> n >> m;
	vector <int> v;
	for (int i = 0;i < n;i++) {
		cin >> p;
		v.push_back(p);
	}
	nth_element(v.begin(), v.begin()+m-1,v.end());
	cout << v.at(m - 1);
	return 0;
}
댓글