Cod sursa(job #1098145)

Utilizator Athena99Anghel Anca Athena99 Data 4 februarie 2014 15:50:08
Problema A+B Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream fin("adunare.in");
ofstream fout("adunare.out");

const int base= 10000;
const int cmax= 10;

void h_write( vector <int> &x ) {
    fout<<x.back();
    for ( int i= (int)x.size()-2; i>=0; --i ) {
        if ( x[i]<10 ) {
            fout<<"000";
        } else if ( x[i]<100 ) {
            fout<<"00";
        } else if ( x[i]<1000 ) {
            fout<<"0";
        }

        fout<<x[i];
    }

    fout<<"\n";
}

void hh_add( vector <int> &x, vector <int> &y ) {
    int t= 0;
    for ( int i= 0; i<(int)x.size() || i<(int)y.size() || t!=0; ++i ) {
        if ( i>=(int)x.size() ) {
            x.push_back(0);
        }
        if ( i<(int)y.size() ) {
            x[i]+= y[i];
        }
        x[i]+= t;
        t= x[i]/base;
        x[i]%= base;
    }
}

void h_turn( int &a, vector <int> &x ) {
    while ( a!=0 ) {
        x.push_back(a%base);
        a/= base;
    }
}

int main(  ) {
    int a, b;
    fin>>a>>b;

    vector <int> x, y;
    h_turn( a, x ), h_turn(b, y );
    hh_add( x, y );
    h_write(x);

    return 0;
}