UVA 11462 Age Sort
2015-07-17 09:37
260 查看
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.
Warning: Input Data is pretty big (∼ 25 MB) so use faster IO.
3 4 2 1 5
5
2 3 2 3 1
0
1 2 2 3 3
实现代码:
排序:
普通三个数排序
1.利用for循环
2.利用三个if语句
3.利用选择排序法
4,利用c++ STL sort()函数
5、利用c++ STL sort() 反序排序
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
53 4 2 1 5
5
2 3 2 3 1
0
Sample Output
1 2 3 4 51 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)
相关文章推荐
- iPhone 6 / 6 Plus 设计·适配方案
- ArcEngle中空值(Null \ Nodata)值的处理
- ACM比赛
- Oracle运维操作集锦[持续更新]
- iOS面试必备看看总有好处
- wget下载FTP目录
- 生成可重集的排列
- Android的Scroller介绍
- editplus打造java运行环境(安装、配置、操作)
- Spring MVC+FreeMarker实现页面按钮权限控制1
- iOS 收起键盘
- CodeForces 548C
- uva 11462 - Age Sort
- exec函数族用法
- iOS初学者
- 五个习惯——让你的工作更高效
- 颜色基础知识——CIE 1931色度坐标
- PHP实现无限级分类的2种方法——父值与左右值2
- 简约之美jodd--props属性使用
- Spring applicationContext.xml文件解析