zoukankan      html  css  js  c++  java
  • 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("
    ");
                
                      
          }
          return 0;
    }
  • 相关阅读:
    jQuery 教程
    单例设计模式getInstance()
    JS和JQuery总结
    Anchor 对象
    static关键字
    HTML、html
    HTML DOM
    HTML DOM 实例Document 对象
    水池进水与放水问题:有一个水池,水池的容量是固定 的500L,一边为进水口,一边为出水口.........(多线程应用)
    迷你DVD管理器(Java版)
  • 原文地址:https://www.cnblogs.com/xinxiangqing/p/4655585.html
Copyright © 2011-2022 走看看