游客 Signup | Login
中文 | En

1520 - a+b

实现一个加法器,使其能够输出a+b的值。

Input

输入包括两个数a和b,其中a和b的位数不超过1000位。

Output

可能有多组测试数据,对于每组数据,

输出a+b的值。

Examples

Input

6 8
2000000000 30000000000000000000

Output

14
30000000002000000000

Solution C

#include<stdio.h>
#include<string.h>
#define N 1010
int main()
{
    char num1[N],num2[N];
    int i,c,k,n,len1,len2;
    while(scanf("%s%s",num1,num2)!=EOF)
	{
        int a[N]={0},b[N]={0};
        len1=strlen(num1);
        len2=strlen(num2);
        if(len1<len2)
            k=len2;
        else
            k=len1;
        c=k;
        for(i=0;i<len1;i++,k--)
            a[k]=num1[len1-1-i]-'0';
        for(i=0,k=c;i<len2;i++,k--)
            b[k]=num2[len2-1-i]-'0';
        for(i=c;i>0;i--)
		{
            a[i]+=b[i];
            if(a[i]>=10)
			{
                a[i]-=10;
                a[i-1]++;
            }
        }
        if(a[0]!=0)
		{
            for(i=0;i<=c;i++)
                printf("%d",a[i]);
        }
        else
		{
            for(i=1;i<=c;i++)
                printf("%d",a[i]);
        }
        printf("\n");
    }
    return 0;
}


Solution C++

#include<stdio.h>
#include<string.h>

void Add(char *str1,char *str2,char *str3)
{
	int i,j,i1,i2,tmp,carry;
	int len1=strlen(str1),len2=strlen(str2);
	char ch;
	i1=len1-1;
	i2=len2-1;
	j=carry=0;
	for(;i1>=0&&i2>=0;++j,--i1,--i2)
	{
		tmp=str1[i1]-'0'+str2[i2]-'0'+carry;
		carry=tmp/10;
		str3[j]=tmp%10+'0';
	}
	while(i1>=0)
	{
		tmp=str1[i1--]-'0'+carry;
		carry=tmp/10;
		str3[j++]=tmp%10+'0';
	}
	while(i2>=0)
	{
		tmp=str2[i2--]-'0'+carry;
		carry=tmp/10;
		str3[j++]=tmp%10+'0';
	}
	if(carry)
		str3[j++]=carry+'0';
	str3[j]='\0';
	for(i=0,--j;i<j;++i,--j)
	{
		ch=str3[i];
		str3[i]=str3[j];
		str3[j]=ch;
	}
}

int main()
{
	char a[1001],b[1001],ans[1005];
	while(scanf("%s%s",a,b)!=EOF)
	{
		Add(a,b,ans);
		puts(ans);
	}
	return 0;
}
Time Limit 1 second
Memory Limit 32 MB
Discuss Stats
上一题 下一题