sort
sort
Time Limit : 6000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 271 Accepted Submission(s) : 41
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
代码示例
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[1000000];/*第一次数组开在里边,因为数组太大,所以过不了*/
int main()
{
int m,n,i,j;
while(scanf("%d%d",&m,&n)!=EOF)
{
for(i=0;i<m;i++)
scanf("%d",&a[i]);
sort(a,a+m);/*暴力排序*/
for(j=m-1;j>m-n;j--)/*倒序输出*/
{
printf("%d ",a[j]);
}
printf("%d\n",a[m-n]);
}
return 0;
}

浙公网安备 33010602011771号