Pagini recente » Cod sursa (job #2989644) | Cod sursa (job #794524) | Cod sursa (job #1549851) | Cod sursa (job #2601437) | Cod sursa (job #1731786)
#include <iostream>
#include <fstream>
using namespace std;
const int nmax = 10000000;
int a[nmax];
int n;
int A,B,C;
void counting(int a[], int n, int digit);
void radixSort(int a[], int n);
int main()
{
ifstream fin("radixsort.in");
ofstream fout("radixsort.out");
fin >> n;
fin >> A >> B >> C;
a[0] = B;
for(int i=1; i<n; i++)
{
a[i] = (A * a[i-1] + B) % C;
}
radixSort(a,n);
for(int i=0; i<n; i+=10)
{
fout << a[i] << '\n';
}
fin.close();
fout.close();
return 0;
}
void radixSort(int a[], int n)
{
int maxNumber = a[0];
for(int i=1 ; i<n; i++)
if(a[i] > maxNumber)
maxNumber = a[i];
for(int digit = 1; maxNumber/digit > 0; digit*= 10)
{
counting(a,n,digit);
}
}
void counting(int a[], int n, 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];
}
}