close
Content
在小學時我們都做過加法的運算,就是把2個整數靠右對齊然後,由右至左一位一位相加。如果相加的結果大於等於10就有進位(carry)的情況出現。你的任務就是要判斷2個整數相加時產生了幾次進位的情況。這將幫助小學老師分析加法題目的難度。
Input
每一列測試資料有2個正整數,長度均小於10位。最後一列有2個0代表輸入結束。
Output
每列測試資料輸出該2數相加時產生多少次進位,請參考Sample Output。注意進位超過1次時operation有加s
Sample Input #1
123 456 555 555 123 594 0 0
Sample Output #1
No carry operation. 3 carry operations. 1 carry operation.
python:
from sys import stdin
for s in stdin:
a,b=map(int,s.split())
if (a==0 and b==0):break
carry=count=0
while(a>0 or b>0):
sum=a%10 +b%10 +carry
if(sum>9):carry=1
else:carry=0
count+=carry
a//=10
b//=10
if(count==0):print(f'No carry operation.')
elif(count==1):print(f'1 carry operation.')
else:print(f'{count} carry operations.')
文章標籤
全站熱搜