题解 | #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;
    }
}

全部评论

相关推荐

07-03 16:13
嘉应学院 Python
xiaolihuam...:很明显骗子,如果是hr直接约你面试了,哪用得着内推,如果是员工的话,你得多优秀,一线员工直接加你微信,
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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