大数加法

本程序应用C++ STL中的容器stack、list实现,支持多组测试数据

#include
#include
#include
#include
#include
#include
using namespace std;
int main ()
{
string a,b;
while(cin>>a>>b){
stack > s1,s2,res;
int len_a=a.length(),len_b=b.length();
for(int i=0;is1.push(a.at(i)-'0');
}
for(int i=0;is2.push(b.at(i)-'0');
}
int len=min(len_a,len_b),f=0;
for(int i=0;if+=s1.top()+s2.top();
s1.pop();
s2.pop();
res.push(f%10);
f/=10;
}
while(!s1.empty()){
f+=s1.top();
s1.pop();
res.push(f%10);
f/=10;
}
while(!s2.empty()){
f+=s2.top();
s2.pop();
res.push(f%10);
f/=10;
}
while(f){
res.push(f%10);
f/=10;
}
while(!res.empty()){
printf("%d",res.top());
res.pop();
}
printf("\n");
}
return 0;
}

你可能感兴趣的:(C/C++,ACMer)