题解 | Primary Arithmetic

Primary Arithmetic

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

#include <bits/stdc++.h>
using namespace std;

int main() {
    string a, b;
    while (cin >> a >> b) { // 注意 while 处理多个 case
        if (a == "0" && b == "0")break;
        reverse(a.begin(), a.end());
        reverse(b.begin(), b.end());

        int f = 0, ans = 0, l = 0, w;
        while (a[l] || b[l] || f != 0) {
            w = f;
            if (a[l] >= '0' && a[l] <= '9') {
                w += a[l] - '0';
            }
            if (b[l] >= '0' && b[l] <= '9') {
                w += b[l] - '0';
            }

            f = w / 10;
            if (f > 0) {
                ans++;
            }
            l++;
        }
        if (ans == 0) {
            cout << "NO carry operation." << endl;
        } else if (ans == 1) {
            cout << ans << " carry operation." << endl;
        } else {
            cout << ans << " carry operations." << endl;
        }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

面了100年面试不知...:头像换成柯南再试试
点赞 评论 收藏
分享
星航夜空的帆舟:好在还有感谢信,我挂了连感谢信都没有
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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