例题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. 如何在编译内核时添加缺少的固件(随着intel wireless 5100 AGN的 iwlwifi 案例)

    我不知道你在笔记本使用 Linux 在内核编译无线wifi 不能用.我的书"关联 Y450"一个足够的旧书,随着无线网卡: $ lspci | grep Wireless 06:0 ...

  2. rhel5.8-LAMP环境搭建

    一.LAMP安装前的准备   安装环境:rhel5.8  zabbix-2.4.5  php-5.6.8   MySQL5.6.23  libpng-1.5.9 zlib-1.2.7 (apr,apr ...

  3. C#中方法Show.和ShowDialog的使用区别

    show()是非模式窗体. showDialog()是模式窗体. 如果这个时候用Show的话,则会发生的事情是,打开子窗体的同时主窗体又显示出来,而使用ShowDialog()的时候主要当子窗体关闭的 ...

  4. C# 4.0 并行计算部分

    C# 4.0 并行计算部分   c#linq算法多线程list微软   目录(?)[-] C 40 并行计算部分 一简单使用 二 并行循环的中断和跳出 三并行循环中为数组集合添加项 四返回集合运算结果 ...

  5. PHP学习笔记二十一【全局变量】

    <?PHP //定义全局变量 global $a; $a=9; //给全局变量赋值 function test1() { global $a; $a=45; } test1(); echo $a ...

  6. javascript 阻止事件冒泡和阻止默认事件对比

    公司项目有像上图中效果的功能需求这也是很常见功能很简单功能,通过一个小例子和大家聊聊js的事件冒泡和默认事件. 先说说一般的实现方式即使用阻止事件冒泡的方式去做,给input绑定一个click事件(并 ...

  7. Spark IDEA开发环境构建

    本文档基于IEDA构建spark maven应用. date: 2016/8/1 author: wangxl 1.下载IDEA https://www.jetbrains.com/idea/ 2.安 ...

  8. (转) class II

    Overloading operators   Classes, essentially, define new types to be used in C++ code. And types in ...

  9. WebBrowser中取对应的图片资源

    在客户端中使用webbrower控件,控件中已显示网页,对应的图片资源应已下载完,下面从缓存中找到图片资源,两种方法都可 1.GetUrlCacheEntryInfo http://msdn.micr ...

  10. Java 枚举随笔

    public enum Color{ //定义枚举并赋值 RED("红色", 1), Blue("蓝色", 2), Yellow("黄色", ...