UVa 11462 Age Sort (计数排序&快速输入输出)
来源:互联网 发布:软件外包项目 编辑:程序博客网 时间:2024/06/14 05:13
11462 - Age Sort
Time limit: 5.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=460&page=show_problem&problem=2457
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 Output for Sample Input
5
3 4 2 1 5
5
2 3 2 3 1
0
1 2 3 4 5
1 2 2 3 3
Note: The memory limit of this problem is 2 Megabyte Only.
注意注意:内存限制自由2M,所以sort不可用,但数字的范围在1~100间,所以用计数排序
复杂度:O(n)
完整代码:
/*0.158s*/#include<cstdio>#include<cstring>#include<cctype>int buf[10]; // 声明成全局变量可以减小开销inline int readint(){char c = getchar();while (!isdigit(c))c = getchar();int x = 0;while (isdigit(c)){x = x * 10 + (c & 15); // 数字处理优化,少了4msc = getchar();}return x;}inline void writeint(int i){int p = 0;if (i == 0)p++; // 特殊情况:i等于0的时候需要输出0,而不是什么也不输出elsewhile (i){buf[p++] = i % 10;i /= 10;}for (int j = p - 1; j >= 0; j--)putchar('0' + buf[j]); // 逆序输出}int main(void){int n, x, c[101];while (n = readint()){memset(c, 0, sizeof(c));for (int i = 0; i < n; i++)c[readint()]++;int first = 1;for (int i = 1; i <= 100; i++)for (int j = 0; j < c[i]; j++){if (!first)putchar(' ');first = 0;writeint(i);}putchar('\n');}return 0;}
- 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 Age Sort 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
- 采用脱机管理方式来编译android源码
- 如何更改Xcode项目名称
- 把图像存入数据库
- ssh: encountered 1 errors during the transfer
- 俄罗斯方块实现代码
- UVa 11462 Age Sort (计数排序&快速输入输出)
- J2EE环境搭建(一)搭建Web服务器和部署Web应用
- 输入一颗二元查找树,将该树转换为它的镜像
- 指针函数与函数指针的区别
- heroku 云服务器使用
- java XML文件的保存
- IOS应用程序声明周期
- 2013年1-7月电子信息产品进出口总额增长19%
- Oracle的锁表和锁用户