本文主要是介绍hdu——2002——sort,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Problem Description给你n个整数,请按从大到小的顺序输出其中前m大的数。
Input
每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且都处于区间[-500000,500000]的整数。
Output
对每组测试数据按从大到小的顺序输出前m大的数。
Sample Input
5 3
3 -35 92 213 -644
Sample Output
213 92 3
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>using namespace std;
const int M=1000005;
const int N=500000;int hash[M];int main()
{int n,m;while(~scanf("%d%d",&n,&m)){memset(hash,0,sizeof(hash));int temp;for(int i=0;i<n;i++){scanf("%d",&temp);hash[temp+N]=1;}int count=0;for(int i=M;i>=0;i--){if(hash[i]==1){count++;if(count==m){printf("%d\n",i-N);break;}elseprintf("%d ",i-N); }}}return 0;
}
这篇关于hdu——2002——sort的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!