Cod sursa(job #1954481)

Utilizator mateibanuBanu Matei Costin mateibanu Data 5 aprilie 2017 13:59:35
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
#include <fstream>
#include <algorithm>

using namespace std;

#define DIM 8192

ifstream f("algsort.in");
ofstream g("algsort.out");

int pos,v[500001],pos2;
char buf[DIM],buf2[DIM],nr[21];

void next(){
    pos++;
    if (pos==DIM){
        f.read(buf,DIM);
        pos=0;
    }
}

void next2(){
    pos2++;
    if (pos2==DIM){
        g.write(buf2,DIM);
        pos2=0;
    }
}

void cit(int& x){
    x=0;
    while (buf[pos]<'0'||buf[pos]>'9'){
        next();
    }
    while (buf[pos]>='0'&&buf[pos]<='9'){
        x=x*10+buf[pos]-'0';
        next();
    }
}

void scrie(int x){
    int l=0,i;
    if (x==0) {buf2[pos2]='0';next2();}
    while (x){
        nr[l]=x%10+'0';
        x/=10;
        l++;
    }
    l--;
    for (i=l;i>=0;i--){
        buf2[pos2]=nr[i];
        next2();
    }
}

void scrie(char x){
    buf2[pos2]=x;
    next2();
}

int main()
{
    int n,i;
    f.read(buf,DIM);
    cit(n);
    for (i=1;i<=n;i++)
        cit(v[i]);
    sort(v+1,v+n+1);
    for (i=1;i<=n;i++)
        {scrie(v[i]);scrie(' ');}
    g.write(buf2,pos2);
    return 0;
}