A + B Problem II &&http://acm.hdu.edu.cn/showproblem.php?pid=1002
2012-01-06 10:04
549 查看
水题继续。。大数相加。。
#include<iostream>
#include<string>
#include<algorithm>
#include<string.h>
using namespace std;
int a[1001];
int main()
{ int n;
cin>>n;
for(int v=1;v<=n;++v)
{ string s,s1;
cin>>s>>s1;
memset(a,0,sizeof(a));
int len1=s.size();
int len2=s1.size();
int len=max(len1,len2);
for(int i=len1-1;i>=0;--i)
a[i]+=s[len1-1-i]-'0';
for(int i=len2-1;i>=0;--i)
a[i]+=s1[len2-1-i]-'0';
int carry=0;
for(int i=0;i<=len;++i)
{ a[i]+=carry;
carry=a[i]/10;
a[i]%=10;
}
int p;
cout<<"Case "<<v<<":"<<endl;
cout<<s<<" + "<<s1<<" = " ;
for(int i=len;i>=0;--i)
if(a[i]!=0) {p=i;break;}
for(int i=p;i>=0;--i)
cout<<a[i];
cout<<endl;
if(v!=n) cout<<endl;
}return 0;
}
#include<iostream>
#include<string>
#include<algorithm>
#include<string.h>
using namespace std;
int a[1001];
int main()
{ int n;
cin>>n;
for(int v=1;v<=n;++v)
{ string s,s1;
cin>>s>>s1;
memset(a,0,sizeof(a));
int len1=s.size();
int len2=s1.size();
int len=max(len1,len2);
for(int i=len1-1;i>=0;--i)
a[i]+=s[len1-1-i]-'0';
for(int i=len2-1;i>=0;--i)
a[i]+=s1[len2-1-i]-'0';
int carry=0;
for(int i=0;i<=len;++i)
{ a[i]+=carry;
carry=a[i]/10;
a[i]%=10;
}
int p;
cout<<"Case "<<v<<":"<<endl;
cout<<s<<" + "<<s1<<" = " ;
for(int i=len;i>=0;--i)
if(a[i]!=0) {p=i;break;}
for(int i=p;i>=0;--i)
cout<<a[i];
cout<<endl;
if(v!=n) cout<<endl;
}return 0;
}
相关文章推荐
- 找规律。。&&http://acm.hdu.edu.cn/showproblem.php?pid=1097
- Quoit Design&&http://acm.hdu.edu.cn/showproblem.php?pid=1007
- Clairewd’s message&&http://acm.hdu.edu.cn/showproblem.php?pid=4300
- Holedox Eating &&http://acm.hdu.edu.cn/showproblem.php?pid=4302
- http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?cid=963&pid=1019&ojid=1
- HDU Today&&http://acm.hdu.edu.cn/showproblem.php?pid=2112
- http://acm.hdu.edu.cn/showproblem.php?pid=2795&&线段树之求最小区间端点值
- Robberies&&http://acm.hdu.edu.cn/showproblem.php?pid=2955
- Red and Black&&http://acm.hdu.edu.cn/showproblem.php?pid=1312
- http://acm.hdu.edu.cn/showproblem.php?pid=1059&&多重背包
- A C&&http://acm.hdu.edu.cn/showproblem.php?pid=1570
- 最小路径覆盖&&http://acm.hdu.edu.cn/showproblem.php?pid=4160
- Happy 2004&&http://acm.hdu.edu.cn/showproblem.php?pid=1452
- Number Sequence&&http://acm.hdu.edu.cn/showproblem.php?pid=1005
- Text Reverse&&http://acm.hdu.edu.cn/showproblem.php?pid=1062
- http://acm.hdu.edu.cn/showproblem.php?pid=1020&&encoding
- 导弹拦截二&&http://acm.hdu.edu.cn/showproblem.php?pid=1257
- The 36th ACM/ICPC Asia Regional Beijing Site&&http://acm.hdu.edu.cn/showproblem.php?pid=4046
- 二分查找求函数的区间最小值&&http://acm.hdu.edu.cn/showproblem.php?pid=2899
- http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?pid=1004&ojid=1&cid=6866&hide=1&problem=Pro