代码如下:
Python求1000以内的水仙花数
def narcissistic_number(num):
s = str(num)
length = len(s)
if len(s) != 3:
return False
sum_num = 0
for i in s:
sum_num += int(i)**length
return sum_num == num
result = [i for i in range(1000) if narcissistic_number(i)]
print(result)
输出:[153, 370, 371, 407]