Cod sursa(job #1015142)

Utilizator PsychoAlexAlexandru Buicescu PsychoAlex Data 23 octombrie 2013 22:27:17
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <iostream>
#include <fstream>
#include <vector>

std::ifstream fin("algosrt.in");
std::ofstream fout("algsort.out");

void citire(int &n, int vec[])
{
    fin>>n;
    for(int i = 0; i < n; i++)
    {
        fin>>vec[i];
    }
}

void rezolvare(int n, int vec[])
{
    std::vector<int>liste[10];
    int k = 1;
    while(k <= 10)
    {
        for(int i = 0; i < n; i++)
        {
            int x = vec[i], p = 1;
            while(x && p < k)
            {
                x = x/10;
                p++;
            }
            liste[x%10].push_back(vec[i]);
        }
        int u = 0;
        for(int i = 0; i < 10; i++)
        {
            for(int p = 0; p < liste[i].size(); p++)
            {
                vec[u] = liste[i][p];
                u++;
            }
            liste[i].clear();
        }
        k++;
    }

    for(int i = 0; i < n; i++)
    {
        std::cout<<vec[i]<<' ';
    }
    std::cout<<'\n';
}

int main()
{
    int n, vec[500001];
    citire(n, vec);
    rezolvare(n, vec);
    return 0;
}