题解 | #a+b#

a+b

https://www.nowcoder.com/practice/4c39c984ea3848b48e111b8e71ec1dd4

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
struct bign{
    int d[1001];
    int n;
    bign(){
        memset(d, 0, sizeof(d));
        n=0;
    }
};
bign splus(bign a,bign b){
    bign ans;
    int fg=0;
    int i;
    for (i=0; i<(a.n>b.n?a.n:b.n); i++) {
        ans.d[i]=(a.d[i]+b.d[i]+fg)%10;
        fg=(a.d[i]+b.d[i]+fg)/10;
    }
    if (fg!=0) {
        ans.d[i]=fg;
        ans.n=i+1;
    }
    else ans.n=i;
    return ans;
}
bign sbuild(string str){
    bign ans;
    ans.n=str.size();
    for (int i=0; i<str.size(); i++) {
        ans.d[i]=str[str.size()-i-1]-'0';
    }
    return ans;
}
int main() {
    string a,b;
    while (cin >> a >> b) { 
        bign ans=splus(sbuild(a),sbuild(b));
        for (int i=ans.n-1; i>=0; i--) {
            cout<<ans.d[i];
        }
        cout<<endl;
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
程序员小白条:你是沟通了900个,不是投了900份简历,你能投900份,意味着对面都要回复你900次,你早就找到实习了,没亮点就是这样的,别局限地区,时间投的也要早,现在都要7月了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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