版权声明:本文为博主原创文章,转载请注明出处 https://blog.csdn.net/xunalove/article/details/88082576
题目描述
实现一个加法器,使其能够输出a+b的值。
输入
输入包括两个数a和b,其中a和b的位数不超过1000位。
输出
可能有多组测试数据,对于每组数据,
输出a+b的值。
样例输入
6 8 2000000000 30000000000000000000
样例输出
14 30000000002000000000
#include<stdio.h>
#include<string.h>
using namespace std;
struct bign{
int len;
int d[1000];
bign()
{
len = 0;
memset(d,0,sizeof(d));
}
};
bign change(char str[])
{
bign temp;
temp.len = strlen(str);
for(int i=0; i<temp.len; i++)
temp.d[i] = str[temp.len-1-i]-'0';
return temp;
}
bign add(bign a, bign b)
{
bign c;
int carry = 0;
for(int i=0; i<a.len||i<b.len; i++)
{
int temp = a.d[i]+b.d[i]+carry;
c.d[c.len++] = temp % 10;
carry = temp / 10;
}
if(carry!=0)
{
c.d[c.len++] = carry;
}
return c;
}
int main()
{
char str1[1001], str2[1001];
while(scanf("%s %s",str1, str2)!=EOF)
{
bign a = change(str1);
bign b = change(str2);
bign c = add(a,b);
for(int i=c.len-1; i>=0; i--)
printf("%d",c.d[i]);
printf("\n");
}
}