Cod sursa(job #1990706)

Utilizator cosmo0093Raduta Cosmin cosmo0093 Data 12 iunie 2017 23:52:42
Problema Radix Sort Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.41 kb
#include <iostream>
#include <fstream>
#include <deque>
#include <vector>
#include <string>

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::deque<std::string> myL;
    std::vector<std::string> res;

    std::deque<std::string> *digits = new std::deque<std::string>[10];

    myL.push_back(std::to_string(b));
    int prev(b);
    for (int i(0); i < nV - 1; i++) {
        prev = (a * prev + b) % c;
        myL.push_back(std::to_string(prev));
    }

    int p(1), len, aux, ind;
    while (!myL.empty()) {
        while (!myL.empty()) {
            len = myL.front().size();
            if (len < p) {
                res.push_back(myL.front());
            } else {
                aux = len - p;
                ind = myL.front()[aux] - '0';
                digits[ind].push_back(myL.front());
            }
            myL.pop_front();
        }

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

        p++;
    }

    std::cout << "here\n";

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

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