ACM Age Sort排序

Description

 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 file. Each case starts with an integer n (0 < n ≤ 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

解题思路:首先确定程序循环条件,然后然按照题意先输入n个数字,并把它存在一个数组中,再用sort函数将它排序(sort函数存在于头文件<algorithm>中,它是按从小到大的顺序排序的)排 好序之                  后就要用for循环语句将它输出来,这时要注意空格的输出,可以这样控制空格的输出(首先给定一个first,并将它赋值为1,如果first为1就把它赋值为0;否则就输出空格, 这样就可以保证不                  会输出多余的空格。)

程序代码:

#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn=2000000;
int a[maxn];
int main ()
{
      int n;
    while(scanf("%d",&n)&&n)
      {
            int first=1;
            for(int i=0;i<n;i++)
                  scanf("%d",&a[i]);
            sort(a,a+n);
            for(int k=0;k<n;k++)
            {
                  if(first)
                        first=0;
                  else
                        printf(" ");
                  printf("%d",a[k]);
            }
            printf("\n");
            
                  
      }
      return 0;
}

 

你可能感兴趣的:(sort)