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 <stdio.h>
using namespace std;
const int N=1000005;
int a[N];
int main()
{
int n,m,j,i;
while(~scanf("%d%d",&n,&m))
{
memset(a,0,sizeof(a));
for(i=0; i<n; ++i)
{
scanf("%d",&j);
a[j+500000]=1;
}
for(i=1000000; m>=1; --i)
{
if(a[i])
{
printf("%d",i-500000);
if(m>1) printf(" ");
else puts("");
m--;
}
}
}
return 0;
}
为什么要按位取反才不会超限,真是醉了。