题解 | #查找第K小数#

查找第K小数

https://www.nowcoder.com/practice/204dfa6fcbc8478f993d23f693189ffd

#include <functional>
#include <iostream>
#include <queue>
using namespace std;

int main() {
    int n;
    while(cin>>n){
        priority_queue<int,vector<int>,greater<int>> smallerHeap;
        for(int i=0;i<n;i++){
            int a;
            cin>>a;
            smallerHeap.push(a);
        }
        int o;
        cin>>o;
        int k=1;
        int kmin=smallerHeap.top();
        smallerHeap.pop();
        while(k<o){
            if(kmin==smallerHeap.top()){
                smallerHeap.pop();
                continue;
            }
            else{
                k++;
                kmin=smallerHeap.top();
                smallerHeap.pop();
            }
        }
        cout<<kmin<<endl;

    }
}

全部评论

相关推荐

喜欢疯狂星期四的猫头鹰在研究求职打法:短作业优先
点赞 评论 收藏
分享
03-05 12:52
吉林大学 Java
挣K存W养DOG:他的价值在于把他家里积攒的财富回馈给社会
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务