Cod sursa(job #2194237)

Utilizator alexsandulescuSandulescu Alexandru alexsandulescu Data 12 aprilie 2018 17:26:06
Problema Radix Sort Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("radixsort.in");
ofstream g("radixsort.out");

int N, x, y, z, MAX, M;
vector<int> a, nr[10];
int main()
{
    f >> N >> x >> y >> z;
    a.push_back(y);
    for(int i = 1; i < N; i++)
        a.push_back((x * a[i - 1] + y) % z), MAX = max(MAX, a[i]);
    M = 1;
    while(MAX) M *= 10, MAX /= 10;
    for(int p = 1; p <= M; p *= 10) {
        for(auto i = a.begin(); i != a.end(); i++)
            nr[*i / p % 10].push_back(*i);
        a.clear();
        for(int i = 0; i <= 9; i++) {
            for(auto j = nr[i].begin(); j != nr[i].end(); j++)
                a.push_back(*j);
            nr[i].clear();
        }
    }
    for(auto i = a.begin(); i != a.end(); i += 10)
        g << *i << " ";
    g << "\n";
    return 0;
}