uva11462Age Sort

题意:题目说输入一些年龄,在1-100之间,然后把它们排序后输出。但是限制条件是输入文件约25MB,内存限制是2MB,不能全都读入后再排序。

分析:数字比较小,可以用计数排序,即用一个数组保存每个年龄出现的个数即可。

View Code
 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <string.h>
 4 #define DEBUG
 5 using namespace std;
 6 int main(){
 7 #ifndef DEBUG
 8     freopen("in.txt", "r", stdin);
 9 #endif
10     int n;
11     int c[101];
12     while(scanf("%d", &n)!=EOF && n){
13         memset(c, 0, sizeof(c));
14         int i, j;
15         for(i=0; i<n; i++){
16             int x;
17             scanf("%d", &x);
18             c[x]++;
19         }
20         bool first = true;
21         for(i=1; i<=100; i++){
22             for(j=0; j<c[i]; j++){
23                 if(!first) printf(" ");
24                 first = false;
25                 printf("%d", i);
26             }
27         }
28         printf("\n");
29     }
30     return 0;
31 }
Greatness is never a given, it must be earned.
原文地址:https://www.cnblogs.com/zjutzz/p/2909971.html