HDU 1425 C++使用sort函数

时间:2023-03-10 02:10:36
HDU 1425 C++使用sort函数

sort

Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 45401 Accepted Submission(s): 13123

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 <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int a[1000001];
int cmp (int a,int b) {
return a > b;
}
int main () {
int num1,num2,num=1;
while (scanf("%d%d",&num1,&num2)!=EOF) {
for (int i=1; i<=num1; i++)
scanf("%d",&a[i]);
sort (a+1,a+num1+1,cmp);
for(int i=1; i<=num2; i++) {
if(i>1)printf(" ");
printf("%d",a[i]);
}
printf("\n");
}
return 0;
}

看到的格式的另一种写法,其中使用了sort函数是c++库中的快速排序