• 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
    ",a[n-1]);
    
    }
    return 0;
    }
    
  • 相关阅读:
    单片机期末考试简答题汇总
    单片机期末考试填空题汇总
    世界五百强世硕科技工作经历——05
    世界五百强世硕科技工作经历——04
    8,求2~n的素数和
    7,特殊毕达哥拉斯三元组
    6,连续多位数的最大乘积
    5,打印1~n之间的所有素数
    4,打印1~n之间的盈数
    3,求1~n(10)的最小倍数
  • 原文地址:https://www.cnblogs.com/yilihua/p/4653572.html
Copyright © 2020-2023  润新知