Cod sursa(job #1479038)

Utilizator seby6191David Eusebiu seby6191 Data 30 august 2015 13:43:29
Problema A+B Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <fstream>
#include <vector>
#include <string>
 
using namespace std;
 
ifstream in("adunare.in");
ofstream out("adunare.out");
 
const int baza= 10;
 
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;
        if(x[i]>=baza){
            x[i]-= baza;
            t= 1;
        }
        else t= 0;
    }
}
 
void transforma(string a, vector<int> &x){
    int k;
    for(int i=(int)a.size()-1; i>=0; --i){
        k= a[i]-'0';
        x.push_back(k);
    }
}
 
void scrie(vector <int> x){
    for(int i= (int)x.size()-1; i>=0; i--){
        out<<x[i];
    }
}
 
int main()
{
    vector <int> x, y;
    string a,b;
    getline(in, a);
    getline(in, b);
    transforma(a, x);
    transforma(b, y);
    hh_add(x,y);
    scrie(x);
    in.close();
    out.close();
    return 0;
}