Cod sursa(job #1316646)

Utilizator tweetyMarinescu Ion tweety Data 13 ianuarie 2015 23:10:54
Problema Radix Sort Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.64 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <algorithm>
using namespace std;

#define LIM_MAX 10000000

int N;
int A;
int B;
int C;
int nrcmax;
int vect[LIM_MAX];
queue<int> myQueue[10];

int nrcif(int x)
{
    int nr =  0;

    while (x)
    {
        ++nr;
        x /= 10;
    }

    return nr;
}

void readData()
{
    ifstream in("radixsort.in");
    in >> N >> A >> B >> C;
    in.close();

    vect[1] = B;
    nrcmax = nrcif(vect[1]);
    for (int i = 2; i <= N; ++i)
    {
        vect[i] = (A * vect[i - 1] + B) % C;

        int t = nrcif(vect[i]);
        if (t > nrcmax)
            nrcmax = t;
    }
}

void printData()
{
    ofstream out("radixsort.out");

    for (int i = 1; i <= N; i += 10)
        out << vect[i] << " ";

    out.close();
}

int myPow(int a, int b)
{
    int c = a;

    if (b == 0)
        return 1;

    if (b == 1)
        return a;

    for (int i = 2; i <= b; ++i)
        c *= a;

    return c;
}

void radixSort()
{
    int i;
    int j;
    int m = 10;
    int n = 1;
    int lim = myPow(10, nrcmax);

    while (m <= lim)
    {
        for (i = 1; i <= N; ++i)
            myQueue[(vect[i] % m) / n].push(vect[i]);

        for (i = 0, j = 1; i <= 9; ++i)
            while (!myQueue[i].empty())
            {
                vect[j++] = myQueue[i].front();
                myQueue[i].pop();
            }

        m *= 10;
        n *= 10;
    }
}

void sortData()
{
    //sort(vect + 1, vect + N + 1);
    radixSort();
}

int main()
{
    readData();
    sortData();
    printData();

    return 0;
}