给你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 <stdio.h>
#include <stddef.h>
#include <cstring>
using namespace std;
int a[1000010];
int main()
{
int n,m,t;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(a,0,sizeof(a));
while(n--)
{
scanf("%d",&t);
a[t+500000]++;
}
for(int i=1000000;i>=0;i--)
{
if(a[i]!=0 && m>0)
{
if(m==1) //如果是m个数中的最后一个数字,就不输出空格
printf("%d",i-500000);
else
printf("%d ",i-500000);
m--;
}
if(m<=0) break;
}
printf("\n");
}
return 0;
}