http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1002
1002: A+B (III)
Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 4321 Solved: 1534Description
Your task is to Calculate a
+ b
.
Input
There are multiple test cases. Each test case contains only one line. Each line consists of a pair of integers a
and b
( 1 <= a
, b
<=1016) , separated by a space. Input is followed by a single line with a
= 0, b
= 0, which should not be processed.
Output
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.
Sample Input
1 5 10 20 10000000000000000 10000000000000000 0 0
Sample Output
6 30 20000000000000000
Hint
Source
中南大学第五届大学生程序设计竞赛-热身赛
这个题跟hdu 1002做法是一样的,原先做hdu的时候是帮朋友纠错,就顺着他的思路去做的,这次自己做,总是错,其中有个变量一直打成另一个了,恰巧,我测试的数据还都是对的,感觉碰哒鬼哒,一直以为是自己的逻辑错了,后来再多测了几次数据才发现,有个变量敲错了,我真是服了我自己,每次都犯这种低级错误,而且还找不到错在哪里,我要先去哭一会。。。。。
#include<stdio.h>
#include<string.h>
char a[1000000],b[1000000];
int c[1000000];
int shu(char a)
{
return (a-'0');
}
int main()
{
int i;
int a1,b1,s;
while(scanf("%s %s",&a,&b)!=EOF)
{
if(a[0]=='0'&&b[0]=='0')
{
break;
}
a1=strlen(a);//a字符串的长度
b1=strlen(b);//b字符串的长度
s=(a1>b1)?a1:b1;
int q=s;
memset(c,0,sizeof(c));
for(s; a1>0&&b1>0; s--)
{
c[s]=c[s]+shu(a[--a1])+shu(b[--b1]);
if(c[s]/10)
{
c[s-1]++;
c[s]=c[s]%10;
}
}
while(a1>0)
{
c[s]=c[s]+shu(a[--a1]);
if(c[s]/10)
{
c[s-1]++;
c[s]=c[s]%10;
}
s--;
}
while(b1>0)
{
c[s]=c[s]+shu(b[--b1]);
if(c[s]/10)
{
c[s-1]++;
c[s]=c[s]%10;
}
s--;
}
for(i=0;i<=q;i++)
{
if(i==0&&c[i]==0)
{
i++;
}
printf("%d",c[i]);
}
printf("\n");
}
return 0;
}