1151: 大整数加法
时间限制: 1 Sec 内存限制: 128 MB
提交: 1795 解决: 759
[提交][状态][讨论版][命题人:admin]
题目描述
比利经常会碰到超大整数的加法运算,而普通的计算器上无法进行。因此他想你帮他写一个程序来计算结果。
输入
输入数据有多组。首先输入一个整数T,表示有T组输入。
每组输入两个大整数,并用空格隔开。每个整数最多1000位。没有负数输入。
输出
对于每组输入,输出两个整数的和,单独占一行。
样例输入
2
1 2
112233445566778899 998877665544332211
样例输出
3
1111111111111111110
提示
来源
#include<stdio.h>
#include<string.h>
char m[1022],n[1022];
int c[1022];
int lres;
int lm,ln;
void update()
{
if(c[lres] >=10)
c[++lres]++,c[lres-1] %=10;
else
lres++;
}
int main()
{
int t;
scanf("%d",&t);
getchar();
while(t--)
{
memset(c,0,sizeof(c));
scanf("%s%s",m,n);
lm = strlen(m)-1;
ln = strlen(n)-1;
lres = 0;
while(lm>=0&&ln>=0)
c[lres] += m[lm--]-'0' + n[ln--] - '0',update();
while(lm>=0)
c[lres]+=m[lm--]-'0',update();
while(ln>=0)
c[lres] += n[ln--]-'0',update();
int f = 0;
for(int i=lres; i>=0; i--)
{
if(c[i]!=0) f=1;
if(f == 1)
printf("%d",c[i]);
}
printf("\n");
}
return 0;
}