ACM比赛(11462 Age Sort)

You are given the ages (in years) of all people of a country with at least 1 year of age. You know that
no individual in that country lives for 100 or more years. Now, you are given a very simple task of
sorting all the ages in ascending order.
Input
There are multiple test cases in the input le. Each case starts with an integer
n(0<=n<=200000)
2000000), the
total number of people. In the next line, there are
n
integers indicating the ages. Input is terminated
with a case where
n
= 0. This case should not be processed.
Output
For each case, print a line with
n
space separated integers. These integers are the ages of that country
sorted in ascending order.
Warning:
Input Data is pretty big (

25 MB) so use faster IO.
Sample Input
5
3 4 2 1 5
5
2 3 2 3 1
0
Sample Output
1 2 3 4 5
1 2 2 3 3

程序分析:此题的考点是一个数组数据的排序,值得注意的是此题如果自己写一排序函数(选择法、冒泡法、插入法)会比较耗时导致程序通不过,所以我们可以考虑使用sort函数。还有就是可能会有很多会把数组a开在主函数,非常建议大家不要这样,这样也会很耗时。

程序代码:

#include <cstdio> #include <iostream> #include <algorithm> using namespace std; int a[2000000]; int main( ) {int i,n ,j,t,k; while(scanf("%d",&n)==1&&n) {for(i=0;i<n;i++) cin>>a[i]; sort(a,a+i); for(i=0;i<n-1;i++) printf("%d ",a[i]); printf("%d\n",a[n-1]); } return 0; } 

你可能感兴趣的:(sort)