Admin- Admin
- عدد المساهمات : 413
تاريخ التسجيل : 20/10/2010
من طرف Admin الأربعاء أغسطس 22, 2018 11:52 pm
- الكود:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void display(string s,vector<int>v){
cout<<s<<endl;
vector<int>::iterator it=v.begin();
for(;it!=v.end();it++)
cout<<*it;
cout<<endl;
};
vector<int> full(string s){
vector<int>v;
int a;
for(int i=0;i<s.size();i++)
{a=s[i]-'0';
v.push_back(a);
}
return v;
};
vector<int> add(vector<int>a,vector<int>b){
//display("",a);
//display("............",b);
vector<int>::iterator aa = a.begin();
vector<int>::iterator bb = b.begin();
vector<int>res;
int rest=0 ,m;
for (; aa != a.end(); aa++,bb++ )
{
m=(*aa)+(*bb)+rest;
rest=0;
if(m>=10)
{res.push_back(m%10);
rest=(m/10)%10;
}//if (Outcome>=10)
else
{
res.push_back(m);
}//else
} //for (; aa != a.rend(); aa++,bb++ )
if (bb!=b.end())
{
for (; bb != b.end();bb++ )
{
m=(*bb)+rest;
rest=0;
if(m>=10)
{res.push_back(m%10);
rest=(m/10)%10;
}//if (Outcome>=10)
else
{
res.push_back(m);
}//else
}//for (; bb != b.rend();bb++ )
}//if (bb!=b.rend())
else if (rest!=0)
res.push_back(rest);
//display("res",res);
return res;
};
vector<int> alone(char c,string a, vector<int> zero){
vector<int>v;
for(vector<int>::iterator it=zero.begin();it!=zero.end();it++)
v.push_back(0);
string result;
int boss=c-'0',dependent,Outcome,rest=0;
for(int i=a.size()-1;i>=0;i--)
{ dependent=a[i]-'0';
Outcome=dependent*boss+rest;
rest=0;
if(Outcome>=10)
{v.push_back(Outcome%10);
rest=(Outcome/10)%10;
}//if (Outcome>=10)
else
{
v.push_back(Outcome);
}//else
}//for(int i=0;i<a.size();i++)
if(rest!=0)
v.push_back(rest);
return v;
};
vector<int> Do(string a,string b){
string::reverse_iterator it=b.rbegin();
vector<int>zero;
vector<int>holder;
vector<int>box;
vector<int>finally;
finally=alone(*it,a,zero);
zero.push_back(0);
it++;
for (; it!=b.rend(); ++it)
{
box=alone(*it,a,zero);
if(box.size()<finally.size())
finally=add(box,finally);
else
finally=add(finally,box);
zero.push_back(0);
}
bool q=true;
vector<int>::reverse_iterator gh=finally.rbegin();
//display("f",finally);
while(gh!=finally.rend())
{ if((q==true))
{ if(*gh==0){
gh++;
continue;}
}
cout<<*gh;
q=false;
gh++;
}
return finally;
};
int main()
{
//display("first :",add(full("864200"),full("146914")));
//cout<<endl;
// display("second : :",add(full("146914"),full("864200")));
//cout<<endl;
// display("third : :",add(full("419641"),full("002468")));
vector< vector<int> > v;
string a,b;
cin>>a>>b;
if((a=="0")||(b=="0"))
{
cout<<0;
return 0;
}
if(a.size()>b.size())
Do(a,b);
else
Do(b,a);
return 0;
}