E题hdu 1425 sort

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1425

sort

Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 33461    Accepted Submission(s): 9968


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++提交
 
Author
LL
 
Source
 
题目大意:就是简单的一个排序,排序后输出前m个数。但是由于数据量很大,很多学弟学妹在开数组的时候有问题导致RE,还有很多处理的问题导致TLE
这里注意一个排序的函数sort() 合理的使用这个函数就是从小到大。需要一个头文件#include <algorithm>
这种排序比较省时。但是在这一题并不是很明显,效率不太高。也可以用别的排序方法加快效率。
 
特别注意格式问题,最后一行没有空行!!
 
详见代码。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 
 7 int num[1000000+10];
 8 
 9 int main()
10 {
11     int n,m;
12     while (~scanf("%d%d",&n,&m))
13     {
14         for (int i=1;i<=n;i++)
15             scanf("%d",&num[i]);
16 
17         sort(num+1,num+n+1);
18         for (int i=n;i>n-m+1;i--)
19         {
20             printf ("%d ",num[i]);
21         }
22         printf ("%d",num[n-m+1]);
23         printf ("
");
24     }
25     return 0;
26 }
 
 
原文地址:https://www.cnblogs.com/qq-star/p/4622952.html