题解 | #查找第K小数#
查找第K小数
https://www.nowcoder.com/practice/204dfa6fcbc8478f993d23f693189ffd
#include <iostream>
#include <queue>
using namespace std;
int main(){
int n,k;
while (cin>>n){
priority_queue<int,vector<int>,greater<int>> myQueue;
int num;
for (int i = 0; i < n; ++i) {
cin>>num;
myQueue.push(num);
}
cin>>k;
while (--k){//由于要寻找第k小的数,所以将前面k-1小的数都弹出
num = myQueue.top();
while (myQueue.top()==num)
myQueue.pop();
}
cout<<myQueue.top()<<endl;
}
}
MDPI公司福利 435人发布