0 0
No carry operation.
1 carry operation.
N carry operations.
(N > 1)while
重複輸入兩整數,直到兩者皆為 0
這題因為有限定輸入範圍 < 10 digits
不然需要大數相加的概念
int n, m;
while(scanf("%d %d", &n, &m) && (n || m)){
...
}
while
迴圈控制,若有一數 > 0 便繼續計算;每次迴圈都用 if
檢查個位數 (% 10) 相加是否需要進位,若有則 carry++
,temp
儲存進位值 (1 or 0);每次迴圈都將兩數 ÷ 10,以便計算下次的個位數
while(n || m){
if((n % 10) + (m % 10) + temp >= 10){
carry++;
temp = 1;
}
else{
temp = 0;
}
n = n / 10;
m = m / 10;
}
#include<stdio.h>
int main(){
int n, m;
while(scanf("%d %d", &n, &m) && (n || m)){
int i;
int carry = 0;
int temp = 0;
while(n || m){
if((n % 10) + (m % 10) + temp >= 10){
carry++;
temp = 1;
}
else{
temp = 0;
}
n = n / 10;
m = m / 10;
}
if(carry == 1){
printf("1 carry operation.\n");
}
else if(!carry){
printf("No carry operation.\n");
}
else{
printf("%d carry operations.\n", carry);
}
}
return 0;
}