题目:打印出所有的“水仙花数” ,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。例如: 153 是一个“水仙花数” ,因为 153=1 的三次方+5 的三次方+3 的三次方。

python代码块:

import math
for num in range(100,999):
    a=math.floor(num/100)       #math.floor(1.7)=1  此函数会将小数点后数全去掉,只剩整数部分
    b=math.floor((num-a*100)/10)
    c=math.floor(num-b*10-a*100)

    if pow(a,3)+pow(b,3)+pow(c,3)==num: #a^3+b^3+c^3==num
        print(num)

猜你喜欢

转载自blog.csdn.net/qq_35182128/article/details/86648397