大数加法

题目描述

Now,we know that Yuanfang is the best student in the class. Every time when the teacher comes up with a hard problem and it seems no student can solve it, He always says: "Yuanfang , what do you think?".
       Today, They had another math class,(why always math class?)The teacher said:”Let’s calculate the sum of A+B,but there are something different ,the two integers A and B are very very  large,Yuanfang, what do you think?"
       Yuanfang wanted to solve this problem quickly. So he came to you again, the only programmer he knows, for help.
       You should write a program to calculate A+B to help him.

输入

 There are several test cases.
Each case has two integers A and B (0≤A,B≤10^60)in a line.

输出

 For each test case,output one line containing a result A+B.

示例输入

10000000000000000000001 10000000000000000000001

示例输出

20000000000000000000002


高精度问题
#include<stdio.h>
#include<string.h>
int main()
{
    char st1[100],st2[100];
    int a[100],b[100],n1,n2,k,i,j;
    while(~scanf("%s %s",st1,st2))
    {
        memset(a,0,sizeof(a));//初始化为0
        memset(b,0,sizeof(b));//同上
        n1=strlen(st1);
        n2=strlen(st2);
if(n1==1&&n2==1&&st1[0]=='0'&&st2[0]=='0')
printf("0\n");
        for(i=0;i<n1;i++)
            a[n1-1-i]=st1[i]-'0';
        for(i=0;i<n2;i++)
            b[n2-1-i]=st2[i]-'0';
        k=n1;
        if(n1<n2)
            k=n2;
        for(i=0;i<k;i++)
            a[i]=a[i]+b[i];
        for(i=0;i<k;i++)
        {
            if(a[i]>9)  //进位
            {
                a[i+1]=a[i+1]+a[i]/10;
                a[i]=a[i]%10;
            }
            else
            a[i]=a[i];
        }
        for(j=k;j>=0;j--)
        {
            if(a[j])break;//如果a[j]==0,跳出,返回j值。用来排除前导0;
        }
        for(i=j;i>=0;i--)
        {
            printf("%d",a[i]);
        }
        printf("\n");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值