Cod sursa(job #1990701)

Utilizator cosmo0093Raduta Cosmin cosmo0093 Data 12 iunie 2017 23:23:01
Problema Radix Sort Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <iostream>
#include <fstream>
#include <list>
#include <vector>

int main() {
    std::ifstream fileIn("radixsort.in");
    std::ofstream fileOut("radixsort.out");

    int nV, a, b, c;
    fileIn >> nV >> a >> b >> c;

    //int nV(100), a(12), b(38), c(123);

    std::list<int> myL;
    std::vector<int> res;

    std::list<int> *digits = new std::list<int>[10];

    myL.push_back(b);

    for (int i(0); i < nV - 1; i++) {
        myL.push_back((a * myL.back() + b) % c);
        //std::cout << myL.back() << '\n';
    }

    long long p(1);
    int aux;
    while (!myL.empty()) {
        while (!myL.empty()) {
            aux = myL.front();
            myL.pop_front();
            if (aux < p) {
                res.push_back(aux);
            } else {
                digits[(aux / p) % 10].push_back(aux);
            }
        }

        for (int i(0); i < 10; i++) {
            while (!digits[i].empty()) {
                myL.push_back(digits[i].front());
                digits[i].pop_front();
            }
        }

        p *= 10;
    }

    for (int i(0); i < nV; i += 10) {
        fileOut << res[i] << ' ';
        //std::cout << res[i] << ' ';
    }

    delete[] digits;
    fileIn.close();
    fileOut.close();
    return 0;
}