sort
Time Limit : 6000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 131 Accepted Submission(s) : 27
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暑期集训队练习赛(三)
AC代码:
另一种输出:(某大神的,对于C的输出目前还是菜鸟)
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int a[1000010]= {0};
int main()
{
int m,n,s,i;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0; i<n; i++)
{
scanf("%d",&s);
a[s+500000]=1;
}
for(i=1000000; i>0; i--)
{
if(a[i]==1)
{
printf("%d",i-500000);
m--;
if(m)
printf(" ");
}
if(m==0)
break;
}
printf("\n");
}
return 0;
}
另一种输出:(某大神的,对于C的输出目前还是菜鸟)
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int a[1000010];
int main()
{
memset(a,0,sizeof(a));
int n,m,i,s;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0; i<n; i++)
{
scanf("%d",&s);
a[s+500000]=1;
}
for(i=1000000; m>0; i--)
{
if(a[i]==1)
{
printf("%d",i-500000);
if(m>1)putchar(32);
else putchar(10);
m--;
}
}
}
return 0;
}
学习心得:
做杭电的题真是微醺,虽然是c语言输入输出的菜鸟,经过这些题也不得不学了,cin,cout一用就会超时,but用c的输入输出就通!过!了!苦苦挣扎好久,真相竟然是这样...
另:大数数组赋初值用memset函数,用a[大数]={0},慢,memset按字节赋值,也可以当清零函数用。