文章

35

粉丝

93

获赞

51

访问

13.4k

头像
查找第K小数 题解:c++ map方法
P1383 北京邮电大学
发布于2024年1月11日 20:14
阅读数 395

#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,x,k,count;
    while(cin>>n){
        count = 0;
        map<int,int> mp;//定义map
        for(int i=0;i<n;i++){
            cin>>x;
            mp[x]++;//映射相当于mp[x]=mp[x]+1;
        }
        cin>>k;

        
        for(int i =0;i<mp.size();i++){
            if(mp[i]>0) count++;//count用来计数
            if(count==k) {
                cout<<i;
                break;
            }
        }
        
    }
    return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发