给定一个整数集合,找出第K大的整数。整数集合中可能有重复的元素,对于重复的元素,排名时不重复计入。
举个例子:
假设整数集合为:4 1 9 2 4 8 2 8 7,那么第1大的数是9,第2大的数是8,第3大的数是7。尽管集合有两个8,但不影响7在整个集合中的排名。
第一行为一个整数:K,表示要查找第K大的数第二行为一个整数集合,整数间用空格分隔
第K大的整数
3 4 1 9 2 4 8 2 8 7
7
#include <bits/stdc++.h>
using namespace std;
int main()
{
int target,num;
vector<int>nums;
cin >> target;
while(cin >> num)
nums.push_back(num);
sort(nums.begin(), nums.end(), greater<int>());
int k = 1;
for(int i = 1;i < nums.size();i++)
{
if(nums[i] == nums[i-1])
continue;
if(k++ == target-1)
cout << nums[i] <<endl;
}
return 0;
}