Cod sursa(job #1313260)

Utilizator vlady1997Vlad Bucur vlady1997 Data 10 ianuarie 2015 14:42:21
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3.5 kb
        #include <cstdio>
        #include <deque>
        #define MAXN 10000001
        #define pb push_back
        #define pf pop_front
        using namespace std;
        int sol[MAXN], nr;
        deque <int> stiva0, stiva1, stiva2, stiva3, stiva4, stiva5, stiva6, stiva7, stiva8, stiva9;
        int nrcif (int x)
        {
            int m=x, nr=0;
            while (m>0)
            {
                m/=10;
                nr++;
            }
            return nr;
        }
        int cif_i (int x, int i)
        {
            int m=x, nr=1;
            while (m>0)
            {
                if (nr==i) return m%10;
                m/=10; nr++;
            }
        }
        void goleste_si_toarna_stivele (void)
        {
            nr=0;
            while (!stiva0.empty())
            {
                sol[++nr]=stiva0.front();
                stiva0.pf();
            }
            while (!stiva1.empty())
            {
                sol[++nr]=stiva1.front();
                stiva1.pf();
            }
            while (!stiva2.empty())
            {
                sol[++nr]=stiva2.front();
                stiva2.pf();
            }
            while (!stiva3.empty())
            {
                sol[++nr]=stiva3.front();
                stiva3.pf();
            }
            while (!stiva4.empty())
            {
                sol[++nr]=stiva4.front();
                stiva4.pf();
            }
            while (!stiva5.empty())
            {
                sol[++nr]=stiva5.front();
                stiva5.pf();
            }
            while (!stiva6.empty())
            {
                sol[++nr]=stiva6.front();
                stiva6.pf();
            }
            while (!stiva7.empty())
            {
                sol[++nr]=stiva7.front();
                stiva7.pf();
            }
            while (!stiva8.empty())
            {
                sol[++nr]=stiva8.front();
                stiva8.pf();
            }
            while (!stiva9.empty())
            {
                sol[++nr]=stiva9.front();
                stiva9.pf();
            }
        }
        int main()
        {
            int n, x, y, z, i, j, m, maxcif=0;
            freopen("algsort.in","r",stdin);
            freopen("algsort.out","w",stdout);
            scanf("%d",&n);
            //sol[1]=y; maxcif=nrcif(sol[1]);
            for (i=1; i<=n; i++)
            {
                scanf("%d",&sol[i]);
                //sol[i]=(x*sol[i-1]+y)%z;
                m=nrcif(sol[i]);
                if (m>maxcif) maxcif=m;
            }
            for (i=1; i<=maxcif; i++)
            {
                for (j=1; j<=n; j++)
                {
                    m=cif_i(sol[j],i);
                    if (m==0) stiva0.pb(sol[j]);
                    if (m==1) stiva1.pb(sol[j]);
                    if (m==2) stiva2.pb(sol[j]);
                    if (m==3) stiva3.pb(sol[j]);
                    if (m==4) stiva4.pb(sol[j]);
                    if (m==5) stiva5.pb(sol[j]);
                    if (m==6) stiva6.pb(sol[j]);
                    if (m==7) stiva7.pb(sol[j]);
                    if (m==8) stiva8.pb(sol[j]);
                    if (m==9) stiva9.pb(sol[j]);
                }
                goleste_si_toarna_stivele();
            }
            for (i=1; i<=n; i++) printf("%d ",sol[i]);
            printf("\n");
            fclose(stdin);
            fclose(stdout);
            return 0;
        }