Cod sursa(job #3185813)

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

using namespace std;

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

const int base= 10;

void sh_turn( string &a, vector <int> &b ) {
    for ( int i= (int)a.size()-1; i>=0; --i ) {
        b.push_back((int)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 ) {
    for ( int i= (int)a.size()-1; i>=0; --i ) {
        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;
}