2018.1.4

大数加法(Eoj——1001)

#include <iostream>
#include <cstring>
using namespace std;
int main(){
    while(1){
        char n1[501],n2[501],n11[501],n22[501],result[505],s=0;
        if(!(cin>>n1>>n2)) break;
        int a1=strlen(n1),a2=strlen(n2);
        strcpy(n11,n1);
        strcpy(n22,n2);
        for(int i=0,j=a1-1;n11[i]!='\0';i++,j--){
            n1[i]=n11[j];
        }
        for(int i=0,j=a2-1;n22[i]!='\0';i++,j--){
            n2[i]=n22[j];
        }
        int max=a1>a2?a1:a2;
        memset(n1+a1,'0',sizeof(n1)-a1);
        memset(n2+a2,'0',sizeof(n2)-a2);
        int w=0;
        for(w=0;w<max;w++){
            if(n1[w]+n2[w]+s-'0'>'9'){
                result[w]=s+n1[w]+n2[w]-'9'-1;
                s=1;
            }else{
                result[w]=s+n1[w]+n2[w]-'0';
                s=0;
            }
        }
        if(s==1){
            result[w]='1';
            for(w;w>=0;w--){
                cout<<result[w];
            }
        }else{
            for(w-=1;w>=0;w--){
                cout<<result[w];
            }
        }
        cout<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值