题解 | #Primary Arithmetic#

Primary Arithmetic

https://www.nowcoder.com/practice/c1fb44e931394e6693671f49c899f5de

#include <iostream>

using namespace std;

int main() {
    string x, y;
    while (cin >> x >> y) {
        if (x == "0" && y == "0") break;
        else {
            int res = 0, t = 0;
            for (int i = x.size() - 1, j = y.size() - 1; i >= 0 || j >= 0; i--, j--) {
                if (i >= 0) t += x[i] - '0';
                if (j >= 0) t += y[j] - '0';
                t /= 10; //向高位进位
                if (t > 0) {
                    res++;//记录进位次数
                }
            }
            if (res == 0) cout << "NO carry operation." << endl;
            else if (res == 1) cout << "1 carry operation." << endl;
            else cout << res << " carry operations." << endl;
        }
    }
    return 0;
}

全部评论

相关推荐

09-08 17:17
同济大学 Java
狗不理fe:里面的人劝一句,别来虾,我们部门24校招生淘汰率30%,还有一些人说有一年保护期,不可能!!!
我的秋招日记
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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