sort
Time Limit:1000MS Memory Limit:65536K
Total Submit:90 Accepted:15
Description
Given n integer numbers, you should select the m numbers of the bigger ones and output them from big one to small one.
Input
The input file contains many test cases.
Each case has 2 lines.
The first line consists of 2 integer number n and m ( 0 < n, m < 1000000).
The next line has n numbers, and all numbers between -500000 and -500000.
Output
For each case, you should output the m integer numbers, order by descent.
Sample Input
5 3
3 -35 92 213 -644
Sample Output
213 92 3
Hint
If n=10 m=3
and the 10 numbers are:
10 9 10 8 7 6 5 4 1 2
you should output:
10 10 9
代码如下
#include <stdio.h>
#include <string.h>
main()
{
int a[1000001];
int i ,j ,n ,m, flag=0,count ,num;
while( scanf("%d%d",&n,&m)!=EOF)
{
count =0;
flag=0;
memset(a,0,sizeof(a));
for(i=0;i<n;i++)
{
scanf("%d",&num);
a[num+500000]++;
}
for(i=1000000; ;i--)
{
for(j=a[i];j > 0;j--)
{
printf("%d ",i-500000);
count++;
if(count==m)
{
flag=1;
break;
}
}
if(flag)
{
printf("\n");
break;
}
}
}
}
本地调试通过,但提交的时候显示runtime error,我实在找不出错误了,大家帮忙找下,谢了~!