标签:font The its eve integer col sign 分享 nbsp
Let‘s define digit degree of some positive integer as the number of times we need to replace this number with the sum of its digits until we get to a one digit number.
Given an integer, find its digit degree.
Example
n = 5
, the output should bedigitDegree(n) = 0
;n = 100
, the output should bedigitDegree(n) = 1
.1 + 0 + 0 = 1
.n = 91
, the output should bedigitDegree(n) = 2
.9 + 1 = 10
-> 1 + 0 = 1
.
我的解答:
def digitDegree(n): s = 0 count = 1 if len(str(n)) == 1: return 0 for i in str(n): s += int(i) if len(str(s)) == 1: return 1 while s >= 10: c = 0 for i in str(s): c = c + int(i) s = c return count + 1
def digitDegree(n): d=0 while n>=10: n=sum([int(i) for i in str(n)]) d+=1 return d
标签:font The its eve integer col sign 分享 nbsp
原文地址:https://www.cnblogs.com/YD2018/p/9499175.html