Pagini recente » Cod sursa (job #2388979) | Cod sursa (job #3128668) | Cod sursa (job #2050672) | Cod sursa (job #2313557) | Cod sursa (job #1109854)
#include <cstdio>
#include <algorithm>
#include <cstring>
#define NMAX 500005
#define pii pair <int, int>
#define x first
#define y second
#define ll long long
#define LMAX 10
#define pb push_back
using namespace std;
int n, A[NMAX], P[NMAX], cnt[LMAX][LMAX];
int *pos[LMAX];
int main()
{
freopen("algsort.in", "r", stdin);
freopen("algsort.out", "w", stdout);
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &A[i]);
for (int i = 1; i <= n; i++)
{
P[i] = i;
for (int j = 1, x = A[i]; j < LMAX; j++, x /= 10)
cnt[j][x % 10]++;
}
for (int i = 1, curr = 1; i < LMAX; i++, curr *= 10)
{
for (int j = 0; j < LMAX; j++)
{
if (pos[j] != NULL) delete pos[j];
pos[j] = new int[cnt[i][j] + 1];
memset(pos[j], 0, sizeof(pos[j]));
}
for (int j = 1; j <= n; j++)
{
int x = (A[P[j]] / curr) % 10;
pos[x][++pos[x][0]] = P[j];
}
for (int j = 0, r = 0; j < LMAX; j++)
for (int k = 1; k <= pos[j][0]; k++)
P[++r] = pos[j][k];
}
for (int i = 1; i <= n; i++)
printf("%d ", A[P[i]]);
printf("\n");
return 0;
}