Python:水仙花数

#coding:utf-8
'''
如果一个 3 位数等于其各位数字的立方和,则称这个数为水仙花数。
例如:153 = 1^3 + 5^3 + 3^3,因此 153 就是一个水仙花数
那么问题来了,求1000以内的水仙花数(3位数) 
'''

#方法一 sxh = [] for i in range(100, 1000): s = 0 l = list(str(i)) for j in l: s += int(j)**len(l) if i == s: sxh.append(i) print("1000以内的水仙花数有:%s" % sxh)

#方法二 a = [] for i in range(100, 1000): b = list(str(i)) if i == pow(int(b[0]), 3) + pow(int(b[1]), 3) + pow(int(b[2]), 3): a.append(i) print("1000以内的水仙花数有:%s" % a)
原文地址:https://www.cnblogs.com/JodieRao/p/12635919.html