B |
Age Sort Input: Standard Input Output: Standard Output |
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.
Problem Setter: Mohammad Mahmudur Rahman
Special Thanks: Shahriar Manzoor
题解:排序个数N=2000000,快排显然不行,因为题目中说人的寿命都会小于100岁,因此重复的很多,用计数排序可以完美的解决此问题。
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 long a[105]; 5 long n; 6 int main(void) 7 { 8 long i,k,flag; 9 while(scanf("%ld",&n)!=EOF) 10 { 11 if(n==0) break; 12 memset(a,0,sizeof(a)); 13 for(i=0; i<n; i++) 14 { 15 scanf("%ld",&k); 16 a[k]++; 17 } 18 flag=1; 19 for(i=1; i<=100; i++) 20 while(a[i]) 21 { 22 if(flag) 23 { 24 printf("%ld",i); 25 flag=0; 26 } 27 else 28 printf(" %ld",i); 29 a[i]--; 30 } 31 printf("\n"); 32 } 33 return 0; 34 }