题意:现在给你若干行年龄段,每段年龄段有n的人的年龄,你的任务是把这些人的年龄按升序排序。
输入格式: 若干行,每一行有一个n,接下来有n个数,代表这n个人的年龄,一直读入,直到n==0才停止。
输出格式: 每行n个数,数与数之间有空格。
注意:输入数据十分大(~25MB),所以要使用更快的IO。
Translated by @(T_T)
输入输出样例
输入 #1
5
3 4 2 1 5
5
2 3 2 3 1
0
输出 #1
1 2 3 4 5
1 2 2 3 3
- 由于每个数字都很小
0~100
,可以使用计数排序 - 用一个
int[128]
每个数组元素a[i]
记录i
的出现次数
#define debug
#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#define MAXN ((int)2e6+7)
#define ll long long
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)
using namespace std;
#define show(x...) \
do { \
cout << "\033[31;1m " << #x << " -> "; \
err(x); \
} while (0)
void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
namespace FastIO {
char print_f[105];
void read() { }
void print() { putchar('\n'); }
template <typename T, typename... T2>
inline void read(T &x, T2 &... oth) {
x = 0;
char ch = getchar();
ll f = 1;
while (!isdigit(ch)) {
if (ch == '-') f *= -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - 48;
ch = getchar();
}
x *= f;
read(oth...);
}
template <typename T, typename... T2>
inline void print(T x, T2... oth) {
ll p3=-1;
if(x<0) putchar('-'), x=-x;
do{
print_f[++p3] = x%10 + 48;
} while(x/=10);
while(p3>=0) putchar(print_f[p3--]);
// putchar(' ');
print(oth...);
}
} // namespace FastIO
using FastIO::print;
using FastIO::read;
int n, m, Q, K, a[MAXN], cnt[128];
int main() {
#ifdef debug
freopen("test", "r", stdin);
// freopen("out_main", "w", stdout);
clock_t stime = clock();
#endif
while((read(n), n) ) {
int x;
for(int i=1; i<=n; i++) {
read(x);
cnt[x] ++;
}
int p = 0;
for(int i=1; i<128; i++)
for(int j=1; j<=cnt[i]; j++) {
if(p++) putchar(' ');
printf("%d", i);
}
putchar('\n');
memset(cnt, 0, sizeof(cnt));
}
#ifdef debug
clock_t etime = clock();
printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif
return 0;
}