Pagini recente » Cod sursa (job #931190) | Cod sursa (job #2141960) | Cod sursa (job #164154) | Cod sursa (job #44411) | Cod sursa (job #2125999)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
int v[10000001];
int main()
{
ifstream in("radixsort.in");
ofstream out("radixsort.out");
int n, a, b, c;
in >> n >> a >> b >> c;
int maxVal = b;
v[1] = b;
for(size_t i = 2; i <= n; i++) {
v[i] = (a * v[i-1] + b)%c;
maxVal = ((v[i]>maxVal)?v[i]:maxVal);
}
int maxIteration = 0;
for(; maxVal > 0; maxVal /= 10) {
maxIteration++;
}
queue<int> buckets[10];
for(size_t iteration = 0; iteration < maxIteration; iteration++) {
for(int i = 1; i <= n; i++) {
int base_div = ((iteration == 0)? 1 : iteration*10);
int digit = (v[i]/base_div)%10;
buckets[digit].push(v[i]);
}
int index = 1;
for(size_t i = 0; i < 10; i++) {
while(!buckets[i].empty()) {
v[index++] = buckets[i].front();
buckets[i].pop();
}
}
}
for(size_t i = 1; i <= n; i++) {
if((i-1) % 10 == 0) {
out << v[i] << " ";
}
}
return 0;
}