时间:2016-03-19 20:22:57 星期六
题目编号:[2016-03-19][UVA][11462][Age Sort]
题目大意:给定n个数字,输出排序后的数据,
分析:n<=2*1E6,多组数据,所以不能直接读取然后排序,这里的数据内容范围是1~100,则可以通过计数的方式来统计,或者自己编写读取和输出的函数来优化
方法2
#include <cstdio>#include <cstring>using namespace std;typedef long long LL;#define CLR(x,y) memset((x),(y),sizeof((x)))#define FOR(x,y,z) for(int (x)=(y);(x)<(z);++(x))int cntAge[101];int main(){ int n; while(~scanf("%d",&n) && n){ CLR(cntAge,0); int a; FOR(i,0,n){ scanf("%d",&a); ++cntAge[a]; } int flg = 0; FOR(i,0,101){ FOR(j,0,cntAge[i]){ if(flg) printf(" "); printf("%d",i); flg = 1; } } puts(""); } return 0;}
#include <cstdio>#include <cstring>#include <cctype>using namespace std;typedef long long LL;#define CLR(x,y) memset((x),(y),sizeof((x)))#define FOR(x,y,z) for(int (x)=(y);(x)<(z);++(x))#define FORD(x,y,z) for(int (x)=(y);(x)>=(z);--(x))int cntAge[101];inline int readInt(){ char ch; do{ ch = getchar(); }while(!isdigit(ch)); int num = 0; while(isdigit(ch)){ num = num * 10 + ch - '0'; ch = getchar(); } return num;}int str[20];inline void writeInt(int a){ int cur = 0; while (a){ str[cur++] = a % 10; a /= 10; } FORD(i,cur - 1,0){ putchar('0' + str[i]); }}int main(){ int n; while(n = readInt()){ CLR(cntAge,0); FOR(i,0,n){ ++cntAge[readInt()]; } int flg = 0; FOR(i,0,101){ FOR(j,0,cntAge[i]){ if(flg) printf(" "); writeInt(i); flg = 1; } } puts(""); } return 0;}