Cod sursa(job #1098149)

Utilizator gedicaAlpaca Gedit gedica Data 4 februarie 2014 15:54:12
Problema A+B Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include<fstream>
#include<vector>

using namespace std;

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

const int base=10;

void hh_adun(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;
        if(x[i]>base)
        {
            x[i]-= base;
            t =1;
        }
        else t= 0;
    }
}

int main()
{
    int a, b;
    vector <int> x;
    vector <int> y;
    in>>a>>b;
    while(a!=0)
    {
        x.push_back(a%10);
        a=a/10;
    }
    while(b!=0)
    {
        y.push_back(b%10);
        b=b/10;
    }
    hh_adun(x,y);
    for(int i=(int)x.size()-1;i>=0;--i)
    {
        out<<x[i];
    }
    in.close();
    out.close();
    return 0;
}