您的位置:首页 > 其它

使用Hash思想,实现输出n个整数的前m大的所有数

2018-01-04 11:32 239 查看

题目:使用Hash思想,实现输出n个整数的前n1大的所有数

输入要求:第一行输入n,n1(n,n1都要求大于-500000小于500000),第二行包含n个互不相同,且处于-500000~500000之间,第三行输出结果

输入样例:5 ,3
    -3 44 11 -55 33
样例输出:44 33

实现代码:
#include <iostream>
#define MAXNUM 500000
int a[1000000];
using namespace std;
int main(int argc, char *argv[])
{
int n,n1,tmp=2*MAXNUM;
while(scanf("%d%d",&n,&n1)!=EOF){
for(int i=0;i<=tmp;i++){
a[i]=0;
}
for(int i=0;i<n;i++){
int x;
scanf("%d",&x);
a[x+MAXNUM]=1;
}
for(int i=500000;i>=-500000;i--){
if(a[i+MAXNUM]==1){
cout<<i;
n1--;
if(n1!=0) printf(" ");
else{
cout<<endl;
break;
}
}
}
}
return 0;
}
测试输出:
4
3
33 44 22 99
99 44 33
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐