题解 | 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")

查看28道真题和解析