uva11462 Age Sort ——计数排序

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2457

题目大意:

  给很多个范围在1到100之内的数字,数量最多有2*10^6个,排序输出。

题目思路:

  数组很大,超内存。不能快排。所以采用计数排序

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cmath>
 6 using namespace std;
 7 int a[120];
 8 void solve() {
 9   int n, tmp, i, j;
10   while (~scanf("%d", &n)) {
11     if (!n) break;
12     memset(a, 0, sizeof(a));
13     for (i = 0; i < n; ++i) {
14       scanf("%d", &tmp);
15       a[tmp]++;
16     }
17     bool mrk = true;
18     for (i = 1; i <= 100; ++i) {
19       for (j = 0; j < a[i]; ++j)  {
20         if (!mrk) printf(" ");
21         printf("%d", i);
22         mrk = false;
23       }
24     }
25     printf("\n");
26   }
27 }
28 int main(void) {
29   //freopen("11462.in", "r", stdin);
30   solve();
31   return 0;
32 }

注意空格的输出。

原文地址:https://www.cnblogs.com/liuxueyang/p/3091068.html