close
Content
我們知道要怎麼確定一個整數是不是 9 的倍數-如果它每位數的總和是9的倍數,那它就是9的倍數。這種檢驗的方法其實是一種遞迴的方法,而且我們把這種方法遞迴的深度稱作 N 的 9-degree 。
你的工作就是,給你一個正整數N,判斷他是不是9的倍數,而且如果他是9的倍數你還需要判斷它的 9-degree。
Input
輸入含有多組測試資料。每組測試資料一列包含一個正數 N。
當 N=0 時代表輸入結束;輸入的數最大可以到1000位數。
Output
對於每一組測試資料,請輸出它是否是 9 的倍數及它的 9-degree。輸出格式請參考Sample Output。
Sample Input #1
999999999999999999999 9 9999999999999999999999999999998 837 0
Sample Output #1
999999999999999999999 is a multiple of 9 and has 9-degree 3. 9 is a multiple of 9 and has 9-degree 1. 9999999999999999999999999999998 is not a multiple of 9. 837 is a multiple of 9 and has 9-degree 2.
prthon:
def fun (n,a): total=0 for i in range(len(n)): total+=int(n[i]) a+=1 if total==9:return a elif total<9 :return -1 else:return fun(str(total),a) while True: try: n=input() if n=='0':break ans=fun(n,0) if ans==-1:print(f'{n} is not a multiple of 9.') else:print(f'{n} is a multiple of 9 and has 9-degree {ans}.') except: break
文章標籤
全站熱搜