大数加法
2017-08-26 17:30
148 查看
本程序应用C++ STL中的容器stack、list实现,支持多组测试数据
#include <iostream>
#include <deque>
#include <stack>
#include <queue>
#include <string>
#include <list>
using namespace std;
int main ()
{
string a,b;
while(cin>>a>>b){
stack<int,list<int> > s1,s2,res;
int len_a=a.length(),len_b=b.length();
for(int i=0;i<len_a;i++){
s1.push(a.at(i)-'0');
}
for(int i=0;i<len_b;i++){
s2.push(b.at(i)-'0');
}
int len=min(len_a,len_b),f=0;
for(int i=0;i<len;i++){
f+=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;
}
#include <iostream>
#include <deque>
#include <stack>
#include <queue>
#include <string>
#include <list>
using namespace std;
int main ()
{
string a,b;
while(cin>>a>>b){
stack<int,list<int> > s1,s2,res;
int len_a=a.length(),len_b=b.length();
for(int i=0;i<len_a;i++){
s1.push(a.at(i)-'0');
}
for(int i=0;i<len_b;i++){
s2.push(b.at(i)-'0');
}
int len=min(len_a,len_b),f=0;
for(int i=0;i<len;i++){
f+=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;
}
相关文章推荐
- 51Nod-1005-大数加法
- 大数加法
- 51nod 1005 大数加法
- 51node 1005 大数加法
- HDU 1753 大明A+B (大数加法)
- C语言-大数加法
- Java大数加法运算
- 51Nod--1005 大数加法
- C语言大数加法
- HDU-多个大数加法问题
- hdu 1002之大数加法
- 提取数字字段和大数加法-批处理应用
- 大数的加法
- 效率(大数加法)——《C++编程风格》读书笔记(五)
- poj 3181 数钱dp + 特殊技巧解决大数加法
- POJ1625 DP+AC自动机+大数加法
- 提取数字字段和大数加法-批处理应用
- 大数加法
- 整数大数加法
- HDOJ-1002 用JAVA写大数问题[大数加法]