美文网首页
水仙花数——Python

水仙花数——Python

作者: 顶宝麻麻 | 来源:发表于2019-07-30 11:28 被阅读0次

    水仙花数
    打印出 100-999 所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数 字立方和等于该数本身。
    例如:153 是一个"水仙花数",因为 153=1 的三次方+ 5 的三次方+3 的三次方。

    def sxhFunc():
        sxh = []
        for i in range(100, 1000):
            sum = 0
            numList = list(str(i))
            for j in numList:
                sum += int(j) ** len(numList)
            if i == sum:
                sxh.append(i)
        print('100~1000之内的水仙花数:',sxh)
    sxhFunc()
    

    相关文章

      网友评论

          本文标题:水仙花数——Python

          本文链接:https://www.haomeiwen.com/subject/jpmgrctx.html