Cod sursa(job #1731798)

Utilizator codebreaker24Tivadar Ionut codebreaker24 Data 19 iulie 2016 22:39:51
Problema Radix Sort Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.85 kb
#include <iostream>
#include <fstream>

using namespace std;
const int nmax = 10000001;
int a[nmax];
int temp[nmax];
int counts[10];
int n;
int A,B,C;

void counting(int digit);
void radixSort();

int main()
{
    ifstream fin("radixsort.in");
    ofstream fout("radixsort.out");

    fin >> n;
    fin >> A >> B >> C;
    a[0] = B%C;
    for(int i=1; i<n; i++)
    {
       a[i] = (1LL * A * a[i-1] % C + B) % C;

    }
    radixSort();

    for(int i=0; i<n; i+=10)
    {

        fout << a[i] << " ";
    }

    fin.close();
    fout.close();



    return 0;
}

void radixSort()
{
    int maxNumber = a[0];
    int i;
    for(i=1 ; i<n; i++)
     if(a[i] > maxNumber)
     maxNumber = a[i];

     int* ap , *tempp, *aux;

     ap =a;
     tempp = temp;

    for(int digit = 1;  maxNumber/digit > 0;  digit*= 10)
    {



            for(i=0; i<n; i++)
            {
                counts[(ap[i]/digit)%10]++;

            }

            for(i=1; i<10; i++)
            {
                counts[i] +=counts[i-1];

            }

            for(i=n-1; i>=0; i--)
            {
                tempp[counts[(ap[i]/digit)%10]-1] = ap[i];
                counts[(ap[i]/digit)%10]--;
            }

            for(int i=0;i<10; i++)
                counts[i] =0;

            aux = ap;
            ap = tempp;
            tempp = aux;



    }



}


/*void counting(int digit)
{

    int temp[n];
    int counts[10] = {0};
    int i;

    for(i=0; i<n; i++)
    {
        counts[(a[i]/digit)%10]++;

    }

    for(i=1; i<10; i++)
    {
        counts[i] +=counts[i-1];

    }

    for(i=n-1; i>=0; i--)
    {
        temp[counts[(a[i]/digit)%10]-1] = a[i];
        counts[(a[i]/digit)%10]--;
    }

    for(i=0; i<n; i++)
    {

        a[i] = temp[i];
    }


}*/