求水仙花数
def shuixianhua(x):a=x//100
b=x//10%10
c=x%100
if x==a**3+b**3+c**3:
print(x, end='')
for i in range(100,1000):
shuixianhua(i)
这样为什么结果不对呢? c=x%100这是什么鬼 题目呢? c代表个位的话,表达式应该是c = x % 10 >>> def shuixianhua(x):
a = x // 100
b = x % 100 // 10
c = x % 10
if x == a**3 + b**3 + c**3:
print(x)
>>> for i in range(100,1000):
shuixianhua(i)
153
370
371
407
>>> def isNarcissistic(num): # 水仙花數(Narcissistic number)
def func(num, res = 0):
if num < 10: return num**3
res += ((eval(str(num)[-1])**3) + func(eval(str(num)[:-1]), res))
return res
return func(num) == num
153
370
371
407 浅谈水仙花数题目
https://fishc.com.cn/thread-192863-1-1.html
(出处: 鱼C论坛)
看看这个 , 说不定就懂了
页:
[1]