UVA 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 thatno individual in that country lives for 100 or more years. Now, you are given a very simple task ofsorting 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), thetotal number of people. In the next line, there are n integers indicating the ages. Input is terminatedwith 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 countrysorted 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

实现代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define Num 2000020
int age[Num];

int main()
{
    int n,test;
    while(scanf("%d",&n)==1&&n)
    {
        for(int i=0;i<n;i++)
            scanf("%d",&age[i]);

            sort(age,age+n);

        for(int i=0;i<n-1;i++)
            {
                printf("%d ",age[i]);
                test=i;
            }
            printf("%d\n",age[test+1]);

    }
    return 0;
}

排序:
普通三个数排序
1.利用for循环

for(i=0;i<3;i++)
  if (a>b) 
  { tmp=a;a=b; b=tmp; }
  if (b>c)
  { tmp=b;b=c; c=tmp; }

2.利用三个if语句

if(a>b)
(temp=a;a=b;b=temp)
if(a>c)
(temp=a;a=c;c=temp)
if(b>c)
(temp=b;b=c;c=temp)

3.利用选择排序法

void SetList(int array[],int cnout)
{
    int i,j,k,temp;
    for(i=0;i=cnout-1;i++)
 {
    k=i;
    for(j=1;j<cnout;j++)
   { if(array[k]<array[j])
      k=j;
      temp=array[k];array[k]=array[j];array[j]=temp;
   }
} 

4,利用c++ STL sort()函数

sort(vector.begin(),vector.end())

5、利用c++ STL sort() 反序排序

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

bool Comp(const int &a,const int &b)
{
    if(a!=b)
        return a>b;
    else
        return a>b;
}
int main()
{
    vector<int> num_list;
    for(int i=0;i<10;i++)
        num_list.push_back(i);
    for(int i=0;i<10;i++)
        cout<<num_list[i]<<" ";
    cout<<endl;
    sort(num_list.begin(),num_list.end(),Comp);
        for(int i=0;i<10;i++)
            cout<<num_list[i]<<" " ;

        cout<<endl;

    return 0;
}

![反序实现](http://img.blog.csdn.net/20150717093627586)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值