题目描述
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.
输入
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.
输出
For each case, print a line with n space
separated integers. These integers are the ages of that country sorted in ascending order.
样例输入
5
2 1 4 3 6
3
2 1 30
样例输出
1 2 3 4 6
1 2 3
时间限制: 1
Sec 内存限制: 1
MB
Sec 内存限制: 1
MB
好吧,这么简单的一道题被一个内存卡住了,排了好久之后觉醒了,数据量这么大(0<n<=2000000),1M的内存根本排不下,于是做了个统计,结果还是超限了,大神说用C不能用C++,改了下,果然。。。。。果然够坑啊!!!
惨烈的现实指示我们,一定要注重理论啊。。
#include<stdio.h> int main() { int C[110]={0};//C数组用来计数,C[i]=j 表示数字i出现了j次 int Flag=0;//为了方便输出格式而设定的标志变量 int N,temp,i,j; while(scanf("%d",&N)) { if(N==0) break;//终止条件 for( i=0;i<N;i++) { scanf("%d",&temp);//读入数据 C[temp]+=1;//计数 } for( i=0;i<110;i++)//输出数据 { for( j=0;j<C[i];j++) { if(Flag==0)//输出第一个数据 { printf("%d",i); Flag=1; } else printf(" %d",i); } } printf("\n"); for(i=0;i<110;i++)C[i]=0; //数组归零 Flag=0;//标志变量归零 } return 0; }