32. 整数加法


 时间限制: 1 s

 空间限制: 128000 KB

 题目等级 : 白银 Silver

题解

 查看运行结果

题目描述 Description

已知两个不超过200位的整数m,n,求m+n的值。

输入描述 Input Description

输入m与n。(中间有空格)

输出描述 Output Description

输出m+n的值。

样例输入 Sample Input

样例输入1:

123456789123456789 987654321

样例输入2:

965758347 654235419

样例输出 Sample Output

样例输出1:

123456790111111110

样例输出2:

1619993766

数据范围及提示 Data Size & Hint

输入整数不超过200位。

代码:

#include

using namespace std;

#include

#include

string m,n;

int main()

{

       int a[201],b[201],c[301]={0};

       cin>>m;

       cin>>n;

       a[0]=m.length();

       b[0]=n.length();

       for(int i=1;i<=a[0];++i)

              a[i]=m[a[0]-i]-'0';

    for(int j=1;j<=b[0];++j)

        b[j]=n[b[0]-j]-'0';

 

       int i=1,x=0;

       while(i<=a[0]||i<=b[0])

       {

              c[i]=a[i]+b[i]+x;

              x=c[i]/10;

              c[i]%=10;

              ++i;

       }

       if(x!=0)

       c[i]=x;

       else i--;

       for(int j=i;j>=1;--j)

       printf("%d",c[j]);

      

       return 0; 

}

转载于:https://www.cnblogs.com/csgc0131123/p/5290465.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值