1. 编写一个程序,求 100~999 之间的所有水仙花数。
如果一个 3 位数等于其各位数字的立方和,则称这个数为水仙花数。例如:153 = 1^3 + 5^3 + 3^3,因此 153 就是一个水仙花数。
思路一:简单,爆力,思路清晰
for i
in range(100,1000
):
temp =
i
temp1 = (temp%10)**3
#求i的个位数
temp2 = (temp//10//10)**3
#求i的百位数
temp3 = (temp//10%10)**3
#求i的十位数
if temp == temp1 + temp2 +
temp3:
print(i)
思路二:思路有点复杂
for i
in range(100, 1000
):
sum =
0
temp =
i
while temp:
sum = sum + (temp%10) ** 3
temp //= 10
if sum ==
i:
print(i)
转载于:https://www.cnblogs.com/ouwen/p/7210812.html
相关资源:Python水仙花数