|
10:大整数加法
总时间限制: 1000ms 内存限制: 65536kB描述 求两个不超过200位的非负整数的和。
输入有两行,每行是一个不超过200位的非负整数,可能有多余的前导0。输出一行,即相加后的结果。结果里不能有多余的前导0,即如果结果是342,那么就不能输出为0342。样例输入
22222222222222222222
33333333333333333333
样例输出
55555555555555555555
来源程序设计实习2007 思路:
模拟;
来,上代码:
#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
char s1[220],s2[220];
void exchange()
{
int l,r;
l=0,r=strlen(s1)-1;
while(r>l)
{
if(s1[l]>='0') s1[l]-='0';
if(s1[r]>='0') s1[r]-='0';
swap(s1[l],s1[r]);
l++,r--;
}
if(l==r) s1[l]-='0';
l=0,r=strlen(s2)-1;
while(r>l)
{
if(s2[l]>='0') s2[l]-='0';
if(s2[r]>='0') s2[r]-='0';
swap(s2[l],s2[r]);
l++,r--;
}
if(l==r) s2[l]-='0';
}
int main()
{
cin>>s1;
cin>>s2;
exchange();
for(int i=0;i<=219;i++)
{
s1+=s2;
if(s1>9) s1[i+1]+=s1/10;
s1%=10;
}
bool if_zero=true;
for(int i=220;i>=0;i--)
{
if(s1==0) continue;
for(int j=i;j>=0;j--) putchar(s1[j]+'0');
if_zero=false;
break;
}
if(if_zero) putchar('0');
return 0;
} |
|
|