# 【題解】ZeroJudge c014: 10035 – Primary Arithmetic

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=c014
【解題想法】把兩個數字從個位數起逐位相加進位，統計進位的次數。

#include <iostream>
using namespace std;

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n1, n2;
while (cin >> n1 >> n2){
int carry = 0, cnt = 0;
if (n1 == 0 && n2 == 0) break;
while (n1 > 0 || n2 > 0){
int tmp = n1 % 10 + n2 % 10 + carry;
if (tmp >= 10){
carry = tmp / 10;
cnt++;
} else {
carry = 0;
}
n1 /= 10;
n2 /= 10;
}
if (cnt == 0) cout << "No carry operation.\n";
else if (cnt == 1) cout << "1 carry operation.\n";
else cout << cnt << " carry operations.\n";
}
return 0;
}

Python code (credit: Amy Chou)

while True:
n1, n2 = map(str, input().split())
if n1 == '0' and n2 == '0':
break

n1 = n1[::-1]
n2 = n2[::-1]

if len(n1) < len(n2):
n1, n2 = n2, n1

ans = 0
carry = 0
for i in range(len(n1)):
tmp = carry + int(n1[i])
if i < len(n2):
tmp += int(n2[i])
if tmp >= 10:
ans += 1
carry = tmp // 10

if ans == 0:
print("No carry operation.")
elif ans == 1:
print("1 carry operation.")
else:
print(f"{ans} carry operations.")