Cod sursa(job #3353955)

Utilizator adimiclaus15Miclaus Adrian Stefan adimiclaus15 Data 12 mai 2026 21:56:30
Problema Sortare prin comparare Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.54 kb
#include <bits/stdc++.h>
using namespace std;

struct Coada {
    int info;
    Coada* next;
};

void push(Coada* &head, Coada* &last, int x) {
    Coada* p = new Coada();
    p -> info = x;
    p -> next = nullptr;
    if(head == nullptr) {
        head = last = p;
    } else {
        last -> next = p;
        last = p;
    }
}
void pop(Coada* &head, Coada* &last) {
    if(head == last) {
        delete head;
        head = last = nullptr;
    } else {
        Coada* t = head;
        head = head -> next;
        delete t;
    }
}

int front(Coada* head) {
    return head -> info;
}
bool empty(Coada* head) {
    return head == nullptr;
}

int nrc(int x) {
    int res = 0;
    while(x > 0) {
        res++;
        x /= 10;
    }
    return res;
}

const int NMAX = 1e6;
int a[NMAX + 1];

int main() {
    ifstream cin("algsort.in");
    ofstream cout("algsort.out");
    Coada* head[10] = {nullptr};
    Coada* last[10] = {nullptr};
    int n;
    cin >> n;
    int pasi = 0;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        pasi = max(pasi, nrc(a[i]));
    }
    int p = 1;
    for(int i = 1; i <= pasi; i++) {
        for(int j = 1; j <= n; j++) {
            int c = (a[j] / p) % 10;
            push(head[c], last[c], a[j]);
        }
        int nr = 0;
        for(int j = 0; j <= 9; j++) {
            while(!empty(head[j])) {
                nr++;
                a[nr] = front(head[j]);
                pop(head[j], last[j]);
            }
        }
        p = p * 10;
    }
    for(int i = 1; i <= n; i++) {
        cout << a[i] << ' ';
    }

}