python 取水仙花数的问题

什么是水仙花数

“水仙花数”

代码

for x in range(100, 1000):
    # 取个位数
    a = x % 10
    # 取十位数
    b = int(x % 100 / 10)
    # 取百位数
    c = int(x / 100)
    if a**3 + b**3 + c**3 == x:
        print(x)

一个小小的测试,重点在怎么取到各位的数字,我竟然想了很久,参考了多个博客……
为自己的智商感到捉急……

另外可以参考以下博客的答案:
https://blog.csdn.net/xiemanR/article/details/72794397
https://www.jb51.net/article/130851.htm
https://blog.csdn.net/i_peter/article/details/78921701

猜你喜欢

转载自www.cnblogs.com/rookieagle/p/11484148.html