题解 | #查找第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;
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-01 11:47
点赞 评论 收藏
分享
点赞 评论 收藏
分享
白火同学:能。我当初应届沟通了1200,收简历50,面试10左右吧,加油投吧
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务