比赛--年龄排序问题--解题报告

年龄排序

题目大意:

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.

要求:

输入:

输入文件要测试案例。输入整数n(0 < n ≤ 2000000),下一行有n个年龄。当输入0时 ,输入终止,案例没有被处理。

输出:

输出的每个数之间有一个空格,国家的年龄按升序排序。

警告:
 Input Data is pretty big (∼ 25 MB) so use faster IO. 

题目分析:

使用sort 函数进行升序排序,注意sort函数的头文件是<algorithm>.

程序代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 const int maxn=20000000;
 7 int n[2000000];
 8 int a[maxn];                        //大数组要定义在外面
 9 
10 int main ()
11 {
12     int n,i;
13     while(scanf("%d",&n)==1&&n)
14     {
15         for(i=0;i<n;i++)
16             scanf("%d",&a[i]);
17         getchar();                  //吸收最后一个空格
18         sort(a,a+n);                //按升序排序
19         for(i=0;i<n-1;i++)
20             printf("%d ",a[i]);
21         printf("%d",a[n-1]);
22 
23         printf("\n");
24     }
25     return 0;
26 }

心得:

这道题做了好久,其实题目不是很难,在VC里也写对了,但在提交的时候总有一点错误,经过几次修改终于做对了。这提醒我在写程序时一定要注意细节,不要范小错误。

转载于:https://www.cnblogs.com/ttmj865/p/4656481.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值