sort
Time Limit : 6000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 197 Accepted Submission(s) : 31
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
Hint
Hint
请用VC/VC++提交
请用VC/VC++提交
Author
Source
ACM暑期集训队练习赛(三)
此题由于给定了输的范围以及明确了数各不相同所以用桶排序是一种比较合理的算法。
#include <stdio.h>
#include<stdlib.h>
int a[1000001];//大数组定义在全局
int main()
{
int n,m,i,t,k;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0; i<n; i++)//运用桶排序(数各不相同)
{
scanf("%d",&t);
a[t+500000]=1;//元素入桶。
}
k=1;//k不能再这定义,例如:int k=1;在这会出现编译错误,所以在循环外定义。
for(i=1000000;i>=0;i--)
{
if(a[i]!=0)
{
if(k<m)
printf("%d ",i-500000);
else if(k==m)
{
printf("%d\n",i-500000);
}
k++;
a[i]=0;
}
else
continue;
}
}
return 0;
}