Cod sursa(job #2679337)

Utilizator teochess2017Togan Teodor-Bogdan teochess2017 Data 30 noiembrie 2020 12:45:37
Problema Radix Sort Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.7 kb
#include <stdio.h>
#include <stdlib.h>

///am cazut nervos aici

#define MAXN 10000000
#define GRUP 16
#define MASK 65535
#define MAXBITS 32

int v[2][MAXN], frecv[MASK + 1];
int put[10] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};///preinitializam un vector de puteri ale lui 10 ca sa scapam de imnultiri si impartiri

FILE *fin, *fout;

int radixsort(int n){
  int i, nrbits, t, aux, tip = 0, nexttip;
  for(nrbits = 0; nrbits < MAXBITS; nrbits += GRUP){
    nexttip = (tip + 1) % 2;
    for(i = 0; i < n; i++){
      frecv[v[tip][i] >> nrbits & MASK]++;
    }
    t = frecv[0];
    frecv[0] = 0;
    for(i = 1; i <= MASK; i++){
      aux = frecv[i];
      frecv[i] = frecv[i - 1] + t;
      t = aux;
    }
    for(i = 0; i < n; i++){
      v[nexttip][frecv[v[tip][i] >> nrbits & MASK]++] = v[tip][i];
    }
    for(i = 0; i <= MASK; i++){
      frecv[i] = 0;
    }
    tip = nexttip;
  }
  return tip;
}

void writeInt(int n){
  int i, cf;
  if(n == 0){
    fputc('0', fout);
  }else{
    i = 9;
    while(n < put[i]){
      i--;
    }
    while(0 <= i){
      cf = 0;
      while(put[i] <= n){
        cf++;
        n -= put[i];
      }
      i--;
      fputc(cf + '0', fout);
    }
  }
  fputc(' ', fout);
}

int main()
{
    int n, a, b, c, i, tip;
    fin = fopen("radixsort.in", "r");
    fscanf(fin, "%d%d%d%d", &n, &a, &b, &c);
    fclose(fin);
    v[0][0] = b;
    for(i = 1; i < n; i++){
      v[0][i] = ((long long)a * v[0][i - 1] + b) % c;
    }
    tip = radixsort(n);
    fout = fopen("radixsort.out", "w");
    for(i = 0; i < n; i += 10){
      writeInt(v[tip][i]);
    }
    fclose(fout);
    return 0;
}