A + B problem
Description
Calculate A + B.
Input
Each line will contain two integers A and B. Process to end of file. (EOF)
Output
For each case, output A + B in one line.
Sample Input 1
2 3
4 5
Sample Output 1
5
9
题目很简单,但是因为所给数据没给范围,不能用long long储存,所以这一题我们需要模拟一下大数的加法,因为每个整数前都没有符号,所以这一题写起来也比较简单。
贴代码:
#include<stdio.h>
#include<string.h>
int main()
{
char a[1000],b[1000];
int i,s[1000],len1,len2,len,j;
while(scanf("%s%s",a,b)!=EOF) //用字符数组来储存数
{
for(i=0;i<1000;i++)
s[i]=0;
len1=strlen(a);len2=strlen(b);
for(i=len1-1,j=0;i>=0;i--,j++) //从个位开始,将每一位都分别加到整型数组s中,下同
s[j]+=a[i]-'0';
for(i=len2-1,j=0;i>=0;i--,j++)
s[j]+=b[i]-'0';
if(len1>len2) //寻找s的最大长度
len=len1;
else
len=len2;
for(i=0;i<=len;i++) //进位
if(s[i]>9)
{
s[i]-=10;
s[i+1]+=1;
}
if(s[len]!=0) //其实就是在判断最大一位是不是进位了
len+=1;
for(i=len-1;i>=0;i--) //逆序输出
printf("%d",s[i]);
printf("\n");
}
return 0;
}