题解 | #高精度整数加法#抛砖引玉

高精度整数加法

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

#include <iostream>
#include <string>
#include <algorithm>
#include<stack>
using namespace std;

int main() {
    string s1, s2;
    while (cin >> s1 >> s2) {
        string ans ;
        stack<char> st1;
        stack<char> st2;
        for (char c : s1)
            st1.push(c);
        for (char c : s2)
            st2.push(c);

        int flag = 0;
        while (st1.size() != 0 or st2.size() != 0) {

            int temp = 0;
            if (st1.size() != 0) {
                temp += st1.top() - '0';
                st1.pop();
            }
            if (st2.size() != 0) {
                temp += st2.top() - '0';
                st2.pop();
            }
            ans += (temp + flag) % 10 + '0';
            flag = (temp + flag) / 10;
        }
        if (flag == 1 and st1.size() == 0 and st2.size()  == 0)
            ans += '1';
        
        reverse(ans.begin(), ans.end());
        cout << ans << endl;
    }
    return 0;
}

全部评论

相关推荐

牛客37185681...:马德,我感觉这是我面过最恶心的公司,一面是两个女hr,说什么实习前几个月属于试用期,试用期过了才能转成正式实习生,我***笑了,问待遇就是不说,问能不能接受全栈,沙币公司
如果可以选,你最想去哪家...
点赞 评论 收藏
分享
否极泰来来来来:解约赔多少
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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