题解 | #找到数组里的第k大数(C++)#

找到数组里的第k大数(C++)

https://www.nowcoder.com/practice/2d5e11b766654104ac91a54fe3a9f5db

#include<bits/stdc++.h>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
	int n,k;
	vector<int>a;
	// write your code here......
	cin>>n>>k;
	while(n--)
	{
		int x;
		cin>>x;
		a.push_back(x);
	}
	nth_element(a.begin(), a.begin()+k-1, a.end());
	vector<int>::iterator it = a.begin()+k-1;
	cout<<*it<<endl;

	return 0;
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务