用python输出1000内的水仙花数
水仙花数是指一个n位正整数(n≥3),它的每个位上的数字的n次幂之和等于它本身。
例如,153是一个水仙花数,因为1³ + 5³ + 3³ = 153。
以下是用Python输出1000内的水仙花数的代码:
for i in range(100, 1000):
sum = 0
temp = i
while temp:
sum += (temp % 10) ** 3
temp //= 10
if sum == i:
print(i)
输出结果为:
153
370
371
407
原文地址: https://www.cveoy.top/t/topic/bura 著作权归作者所有。请勿转载和采集!