题解 | #高精度整数加法#

高精度整数加法

https://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6

#include <iostream>

using namespace std;

string bigIntAdd(string s1,string s2){
    string res;
    int i=s1.size()-1,j=s2.size()-1;
    int add,carry = 0;
    while (i>=0 && j>=0) {
        add = (s1[i--]-'0') + (s2[j--]-'0') + carry;
        carry = add / 10;
        res = to_string(add%10) + res;
    }
    while (i>=0){
        add = (s1[i--]-'0') + carry;
        carry = add / 10;
        res = to_string(add%10) + res;
    }
    while (j>=0){
        add = (s2[j--]-'0') + carry;
        carry = add / 10;
        res = to_string(add%10) + res;
    }
    while (carry){
        res = to_string(carry) + res;
        carry = 0;
    }
    return res;
}

int main(){
    string s1,s2;
    while (cin>>s1>>s2){
        cout<<bigIntAdd(s1,s2)<<endl;
    }

    return 0;
}

全部评论

相关推荐

彧未sr:查看图片
投递牧原集团等公司7个岗位
点赞 评论 收藏
分享
LazyBreeze:项目尽量体现你对技术的理解和深度,不是说把中间件用一下就完事了,你项目里面提到集群和分布式,你真在服务器上部署过吗,感觉太假了,第二个项目说自己用了微服务的什么组件,只是用了没有自己的思考,很难让面试官注意到你的简历。针对某几个技术点自己多思考一下,考虑一下有没有别的替代方案,可以写一下,即使没有真的实现
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务