例题17  年龄排序(Age Sort, UVa 11462)照从小到大的顺序输出。

【输入格式】

输入包含多组测试数据。每组数据的第一行为整数n(0<n≤2 000 000),即居民总数;下一行包含n个不小于1、不大于100的整数,即各居民的年龄。输入结束标志为n=0。

输入文件约有25MB,而内存限制只有2MB。

【输出格式】

对于每组数据,按照从小到大的顺序输出各居民的年龄,相邻年龄用单个空格隔开。



效率对比:





输入输出挂

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]); //逆序输出
}

1.注意3次getchar

2.注意inline

3.注意i=0;

4.注意buf的全局性

完整代码:

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <cctype>
#define uns unsigned
#define int64 long long
#ifdef WIN32
#define fmt64 "%I64d"
#else
#define fmt64 "%lld"
#endif
#define oo 0x13131313
using namespace std;
int n;
int Sort[200];
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 printfint(int i)
{
int p=0;
if(i==0) p++;
else while(i)
{
buf[p++]=i%10;
i/=10;
}
for(int j=p-1;j>=0;j--) putchar('0'+buf[j]);
}
void output()
{
int tot=0;
for(int i=1; i <= 100; i++)
for(int j=1; j <= Sort[i]; j++)
{
printfint(i);
tot++;
if(tot!=n) printf(" ");
}
printf("\n");
}
void input()
{
int temp;
while(scanf("%d",&n)!=EOF&&n!=0)
{
memset(Sort,0,sizeof(Sort));
for(int i=1; i <= n ; i++ )
{
temp=readint();
Sort[temp]++;
}
output();
}
} int main()
{
input();
return 0;
}

【输入输出挂】【Uva11462】Age Sort的更多相关文章

  1. UVA 11462 Age Sort(计数排序法 优化输入输出)

    Age Sort You are given the ages (in years) of all people of a country with at least 1 year of age. Y ...

  2. POJ1904(有向图缩点+输入输出挂参考)

    King's Quest Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 8311   Accepted: 3017 Cas ...

  3. poj 2823 Sliding Windows (单调队列+输入输出挂)

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 73426   Accepted: 20849 ...

  4. Uva-------(11462) Age Sort(计数排序)

    B Age Sort Input: Standard Input Output: Standard Output   You are given the ages (in years) of all ...

  5. COGS 1406. 邻居年龄排序[Age Sort,UVa 11462](水题日常)

    ★   输入文件:AgeSort.in   输出文件:AgeSort.out   简单对比时间限制:1 s   内存限制:2 MB [题目描述] Mr.Zero(CH)喜闻乐见地得到了一台内存大大增强 ...

  6. ACM比赛(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 t ...

  7. 【UVA 11462】 Age Sort(基数排序)

    题 题意 给你最多2000000个数据,大小是1到99的数,让你排序输出. 分析 快排也可以过.不过这题本意是要基数排序(桶排序),就是读入年龄age, a[age]++,然后输出时,从1到99岁(看 ...

  8. UVa 11462 Age Sort

    解题报告:给若干个居民的年龄排序,年龄的范围在1到100之间,输入的总人数在0到200W.这题要注意的输入的文件约有25MB,而内存限制为2MB,所以如果人数是像200W这样多的话,甚至都不能把它们都 ...

  9. 11462 Age Sort(计数排序)

    内存不够用,用计数排序可以解决问题. #include<iostream> #include<cstdio> #include<cstdlib> #include& ...

随机推荐

  1. [Regular Expressions] Find the Start and End of Whole Words

    Regular Expression Word Boundaries allow to perform "whole word only" searches within our ...

  2. linux重命名

    mv  A  B 将目录A重命名为B mv  /a  /b /c   将目录/a目录移动到/b下并重命名为c 其实在文本模式中要重命名文件或目录的话也是很简单的,我们只需要使用mv命令就可以了,比如说 ...

  3. css盒子模型,定位,浮动

    1.盒子模型 Margin(外边距) - 清除边框外的区域,外边距是透明的. Border(边框) - 围绕在内边距和内容外的边框. Padding(内边距) - 清除内容周围的区域,内边距是透明的. ...

  4. struts2 注解方式

    struts2扫描方法: 扫描其位于包的命名注解的类 “struts, struts2, action 或 actions“. 接着,扫描相匹配下列任一条件的文件: 实例了 com.opensymph ...

  5. (原创) mac 10.9.2 eclipse 的 CDT 的 异常的修复

    测试平台:macbook air 2012 , os x 10.9.2 , eclipse 4.3   在升级了 10.9 之后,eclipse 的CDT 无法正常使用了   异常表现:   1. 文 ...

  6. HTML5简单入门系列(八)

    前言 本篇介绍HTML5中相对复杂的一些APIs,其中的数学知识比较多.虽然如此,但是其实API使用起来还是比较方便的. 这里说明一下,只写出API相关的JS代码,因为他们都是基于一个canvas标签 ...

  7. jquery 单选框整个选中

    问题:遇到单选框,如图 解决办法:利用jqurey click->checked <!DOCTYPE html> <html lang="en"> & ...

  8. python 连接操作数据库(一)

    一.下面我们所说的就是连接mysql的应用: 1.其实在python中连接操作mysql的模块有多个,在这里我只给大家演示pymysql这一个模块(其实我是感觉它比较好用而已): pymysql是第三 ...

  9. shell脚本分类

    shell脚本分为三类:登录脚本.交互式脚本.非交互式脚本 一. 登录脚本类似于windows下的计算机设置中的登录脚本和账户设置下的登录脚本的合集(我是这么理解的哈). 其配置文件的关键词为pref ...

  10. R语言数据合并使用merge数据追加使用rbind和cbind

    R语言中的横向数据合并merge及纵向数据合并rbind的使用 我们经常会遇到两个数据框拥有相同的时间或观测值,但这些列却不尽相同.处理的办法就是使用merge(x, y ,by.x = ,by.y ...