UVA - 11462 - Age Sort (高效算法!!)
来源:互联网 发布:c语言自学视频 编辑:程序博客网 时间:2024/05/22 06:50
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 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
思路:计数排序
AC代码1(519ms):
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int main(){int x, n, c[101];while(scanf("%d", &n) == 1 && n){memset(c, 0, sizeof(c));for(int i=0; i<n; i++){scanf("%d", &x);c[x]++;}int flag = 1;for(int i=1; i<=100; i++)for(int j=0; j<c[i]; j++){if(!flag) printf(" ");flag = 0;printf("%d", i);}printf("\n");} return 0;}
AC代码2(172ms):
#include <cstdio>#include <cstring>#include <algorithm>#include <cctype> //为了使用isdigit宏 using namespace std;inline int readint(){char c = getchar();while(!isdigit(c)) c = getchar();int x = 0;while(isdigit(c)){x = x * 10 + c - '0';c = getchar();}return x;}int buf[10];//声明成全局变量可以减小开销 inline void writeint(int i){int p = 0;if(i == 0) p++;//特殊情况:i等于0的时候需要输出0,而不是什么也不输出 else while(i){buf[p++] = i % 10;i /= 10;}for(int j = p-1; j >= 0; j--) putchar('0' + buf[j]);//逆序输出 }int main(){int n, x, c[101];while(n = readint()){memset(c, 0, sizeof(c));for(int i = 0; i < n; i++) c[readint()]++;int flag = 1;for(int i = 1; i <= 100; i++)for(int j=0; j < c[i]; j++){if(!flag) putchar(' ');flag = 0;writeint(i);}putchar('\n');}return 0;}
- UVA - 11462 - Age Sort (高效算法!!)
- ( 设计高效算法 ) 年龄排序 Age Sort Uva 11462
- UVa 11462 - Age Sort
- Uva-11462-Age Sort
- uva 11462 Age Sort
- Uva - 11462 - Age Sort
- UVA 11462 Age Sort
- UVa:11462 Age Sort
- UVA - 11462 Age Sort
- UVA 11462 - Age Sort
- UVA 11462 Age Sort
- UVa 11462 - Age Sort
- uva 11462 Age Sort
- UVA 11462 Age Sort
- UVA - 11462 Age Sort
- UVA - 11462 Age Sort
- UVA - 11462 Age Sort
- uva 11462 - Age Sort
- Linux/Android——input_handler之evdev (四)
- pro-- 通讯录3-Edit
- spring 在普通类中调用注入的对象
- 让Win服务已当前登录用户的身份来执行函数
- LINUX命令总结
- UVA - 11462 - Age Sort (高效算法!!)
- 九度OJ 区间问题
- 查询转换——使用物化视图进行查询重写(4)!
- 安装minicom之后配置界面很乱,解决方法
- Weblogic 10.3.5在64位Windows系统下的安装和配置
- 实验三
- 根据网上OLEDB读写excel文件改写
- 【笔记】Java设计模式——绪论
- 深入学习Tomcat----自己动手写服务器(附服务器源码)