看不懂这个代码
def Narcissus():for each in range(100, 1000):
temp = each
sum = 0
while temp:
sum = sum + (temp%10) ** 3
temp = temp // 10# 注意这里用地板除
if sum == each:
print(each, end='\t')
print("所有的水仙花数分别是:", end='')
Narcissus() 题目是:寻找水仙花数
题目要求:如果一个3位数等于其各位数字的立方和,则称这个数为水仙花数。例如153 = 1^3+5^3+3^3,因此153是一个水仙花数。编写一个程序,找出所有的水仙花数
这串代码我看不懂求大神详细解释下
看了之前的基础 感觉这个代码我还是很难理解 求大神分析下原因在哪里 https://fishc.com.cn/forum.php?mod=viewthread&tid=123695&highlight=%CB%AE%CF%C9%BB%A8 我是这样实现的
number=[]
temp = 100
while temp < 1000:
sum =(temp%10)**3+((temp//10)%10)**3+(temp//100)**3
if sum == temp:
number.append(temp)
temp +=1
print(number)
def Narcissus():
for each in range(100, 1000):
temp = each
sum = 0
while temp:
sum = sum + (temp%10) ** 3
temp = temp // 10# 注意这里用地板除
#这里循环一个数会while循环3次,第一次循环得到个位数的立方,第二次得到十位数的立方并加上个位的,第三次会得到百位数的立方加上前面的,到第四次temp就是0了,之后从for循环取数值
if sum == each:
print(each, end='\t') isincere 发表于 2018-11-6 15:08
def Narcissus():
for each in range(100, 1000):
temp = each
为什么 while 0程序就中断了呢 a2421480 发表于 2018-11-7 02:23
为什么 while 0程序就中断了呢
while temp: #如果 temp 为 0,退出循环 isincere 发表于 2018-11-9 11:06
while temp: #如果 temp 为 0,退出循环
我就是问你啊为什么temp=0就退出循环了呢{:5_96:}
页:
[1]