HDU - 1425 sort
作者:互联网
sort
给你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
本题思路:就是给你n个数,然后进行排序有大到小的输出m个数,本题可以直接用sort排序,然后倒序输出m个数即完成输出。(注:本题要用scanf,printf,否则要TLE)
AC代码如下:
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxn = 1e6 + 5;
int a[maxn];
int main()
{
int n, m;
while (~scanf("%d%d", &n, &m))
{
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);//5 3
sort(a+1, a + n + 1);
/*for (int i = 1; i <= n; i++)
cout << a[i] << "*";*/
for (int i = n; i > n - m+1; i--)
{
printf("%d ", a[i]);
}
printf("%d\n", a[n - m + 1]);
}
return 0;
}
标签:sort,输出,HDU,1425,int,printf,include,本题 来源: https://blog.csdn.net/lsdstone/article/details/101113321