打印出 100-999 所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字 立方和等于该数本身。例如:153 是一个"水仙花数",因为 153=1 的三次方+ 5 的三次方+3 的三次方。
flower_num = []
for num in range(100, 1000):
lis = list(str(num)) # 获取每位上的数字字符串
s = 0
for i in lis:
s += int(i) ** len(lis) # 求三次方和
if num == s:
flower_num.append(num)
print('水仙花数:%s' % flower_num)
输出
水仙花数:[153, 370, 371, 407]