【範例】計數排序 counting sort
【題目敘述】https://zerojudge.tw/ShowProblem?problemid=d190
#include <iostream>
#include <cstring>
using namespace std;
int a[100];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, tmp;
while (cin >> n){
if (n == 0) break;
memset(a, 0, sizeof(a));
for (int i=0; i<n; i++){
cin >> tmp;
a[tmp]++;
}
for (int i=0; i<100; i++){
while (a[i]--){
cout << i << ' ';
}
}
cout << '\n';
}
return 0;
}
Python code (credit: Amy Chou)
【題目敘述】https://vjudge.net/problem/UVA-11462
注意:輸出的最後不能有一個空白。
while True:
n = int(input())
if n == 0:
break
A = list(map(int, input().split()))
dic = {}
for a in A:
dic[a] = dic.get(a, 0) + 1
first = True
for k in sorted(dic.keys()):
for _ in range(dic[k]):
if first:
first = False
else:
print(" ", end="")
print(k, end="")
print()