码迷,mamicode.com
首页 > 其他好文 > 详细

Code Signal_练习题_digitDegree

时间:2018-08-19 01:05:48      阅读:140      评论:0      收藏:0      [点我收藏+]

标签: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

    • For n = 5, the output should be
      digitDegree(n) = 0;
    • For n = 100, the output should be
      digitDegree(n) = 1.
      1 + 0 + 0 = 1.
    • For n = 91, the output should be
      digitDegree(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
膜拜大佬

 

Code Signal_练习题_digitDegree

标签:font   The   its   eve   integer   col   sign   分享   nbsp   

原文地址:https://www.cnblogs.com/YD2018/p/9499175.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!