Cod sursa(job #3291552)

Utilizator Ruxandra009Ruxandra Vasilescu Ruxandra009 Data 5 aprilie 2025 09:07:08
Problema Radix Sort Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#include <fstream>
#include <vector>

using namespace std;

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

int n, a, b, c, nrcif;
vector<int> v, aux;


void radsort()
{
    for(int i = 1, p = 1; i <= nrcif; i ++, p *= 10)
    {
        aux.clear();
        for(int i = 0; i < 10; i ++)
            for(auto x : v)
            {
                int c = (x / p) % 10;
                if(c == i)
                    aux.push_back(x);
            }

        v = aux;
    }
}

int main()
{
    f >> n >> a >> b >> c;
    v.push_back(b);
    for(int i = 1; i < n; i ++)
    {
        int x = (a * v.back() + b) % c;
        v.push_back(x);

        int nr = 0;
        while(x)
            x /= 10, nr ++;

        nrcif = max(nrcif, nr);
    }

    radsort();

    for(int i = 0; i < n; i += 10)
        g << v[i] << " ";
    return 0;
}