Cod sursa(job #3185818)

Utilizator aeandreescuAndreescu Ana-Eliza aeandreescu Data 20 decembrie 2023 15:47:01
Problema A+B Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.33 kb
#include <fstream>
#include <string>
#include <vector>

using namespace std;

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

const int base= 10000;

void sh_turn( string &a, vector <int> &b ) {
    b.resize((a.size()+3)/4);
    for ( int i= 0; i<(int)a.size(); ++i ) {
        b[((int)a.size()-i-1)/4]= b[((int)a.size()-i-1)/4]*10+a[i]-'0';
    }
}

void hh_add( vector <int> &a, vector <int> &b ) {
    for ( int i= 0; i<(int)a.size() || i<(int)b.size(); ++i ) {
        if ( i>=(int)a.size() ) {
            a.push_back(0);
        }

        if ( i<(int)b.size() ) {
            a[i]+= b[i];

            if ( a[i]>=base ) {
                a[i]%= base;
                if ( i==(int)a.size()-1 ) {
                    a.push_back(0);
                }
                ++a[i+1];
            }
        }
    }
}

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

int main() {
    string x, y;
    fin>>x>>y;

    vector <int> a, b;
    sh_turn(x, a), sh_turn(y, b);

    hh_add(a, b);
    h_write(a);

    return 0;
}