|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在课后动手题1中要求打印出水仙花数,我写的是这样的程序:
for i in range(100,1000):
if i != ((i//100)**3 + ((i%100)//10)**3 + (i%10)**3):
continue
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)
while循环体想表达的是什么意思看不明白,有大神能解释下不
假设 temp = 123
循环条件是 while temp (等同于 while temp != 0 )
第一次:
sum = 0 +(123%10)**3 = 3**3 = 27
temp=123//10 = 12
第二次:
sum = 27 +(12%10)**3 = 27 + 2**3 = 35
temp = 12 // 10 = 1
第三次:
sum = 35 + (1%10)**3 = 35 +1**3 = 36
temp = 1 // 10 = 0 退出循环
|
|