第十八课课后练习求水仙花数
import timedef FindNarcissus():
start_time = time.time() #记录程序开始时间
for each in range(100,1000):
sum = 0
temp = each
#
while temp: #最终判断temp=0,判定为False
sum += (temp % 10) ** 3
temp = temp // 10
if (sum == each):
print (each)
end_time = time.time()# 记录程序结束时间
print('运行时间: %f 秒' %(end_time - start_time)) #out:运行时间: 0.000999 秒
print(FindNarcissus())https://h5.qzone.qq.com/page/photo?init=photo.v7/common/viewer2/index&picKey=NR8AVjZiQ2dBeE5qSXlOalV6TnpNeVV6KkNYNFpXc3lvIQcAcGhvdG9neg!!&ownerUin=1622653732&appid=4&topicId=V10ROGNG1pvnCX_NR8AVjZiQ2dBeE5qSXlOalV6TnpNeVV6KkNYNFpXc3lvIQcAcGhvdG9neg!!_1606565714544000_4&pre=http%3A%2F%2Fa1.qpic.cn%2Fpsc%3F%2FV10ROGNG1pvnCX%2FruAMsa53pVQWN7FLK88i5rFe4fjq*ZHtlzkaEJTN7*QlCGz0Q3gr9TA6bfoyVg2WHAnERhVTeJj58tqqx*EvO5y8dtDHdJn8fSUsg1aGYZQ!%2Fm%26ek%3D1%26kp%3D1%26pt%3D0%26bo%3D0gNdAtIDXQIDGTw!%26tl%3D1%26vuin%3D1622653732%26tm%3D1606564800%26sce%3D60-3-3%26rf%3D0-0&useqzfl=1&useinterface=1&noCloseBtn=0&inqq=1
https://h5.qzone.qq.com/page/photo?init=photo.v7/common/viewer2/index&picKey=NR8AVjZiQ2dBeE5qSXlOalV6TnpNeVV6KkNYNFpXc3lvIQcAcGhvdG9neg!!&ownerUin=1622653732&appid=4&topicId=V10ROGNG1pvnCX_NR8AVjZiQ2dBeE5qSXlOalV6TnpNeVV6KkNYNFpXc3lvIQcAcGhvdG9neg!!_1606565714544000_4&pre=http%3A%2F%2Fa1.qpic.cn%2Fpsc%3F%2FV10ROGNG1pvnCX%2FruAMsa53pVQWN7FLK88i5rFe4fjq*ZHtlzkaEJTN7*QlCGz0Q3gr9TA6bfoyVg2WHAnERhVTeJj58tqqx*EvO5y8dtDHdJn8fSUsg1aGYZQ!%2Fm%26ek%3D1%26kp%3D1%26pt%3D0%26bo%3D0gNdAtIDXQIDGTw!%26tl%3D1%26vuin%3D1622653732%26tm%3D1606564800%26sce%3D60-3-3%26rf%3D0-0&useqzfl=1&useinterface=1&noCloseBtn=0&inqq=1
https://h5.qzone.qq.com/page/photo?init=photo.v7/common/viewer2/index&picKey=NR8AVjZiQ2dBeE5qSXlOalV6TnpNeVV6KkNYNFpXc3lvIQcAcGhvdG9neg!!&ownerUin=1622653732&appid=4&topicId=V10ROGNG1pvnCX_NR8AVjZiQ2dBeE5qSXlOalV6TnpNeVV6KkNYNFpXc3lvIQcAcGhvdG9neg!!_1606565714544000_4&pre=http%3A%2F%2Fa1.qpic.cn%2Fpsc%3F%2FV10ROGNG1pvnCX%2FruAMsa53pVQWN7FLK88i5rFe4fjq*ZHtlzkaEJTN7*QlCGz0Q3gr9TA6bfoyVg2WHAnERhVTeJj58tqqx*EvO5y8dtDHdJn8fSUsg1aGYZQ!%2Fm%26ek%3D1%26kp%3D1%26pt%3D0%26bo%3D0gNdAtIDXQIDGTw!%26tl%3D1%26vuin%3D1622653732%26tm%3D1606564800%26sce%3D60-3-3%26rf%3D0-0&useqzfl=1&useinterface=1&noCloseBtn=0&inqq=1
https://h5.qzone.qq.com/page/photo?init=photo.v7/common/viewer2/index&picKey=NR8AVjZiQ2dBeE5qSXlOalV6TnpNeVV6KkNYNFpXc3lvIQcAcGhvdG9neg!!&ownerUin=1622653732&appid=4&topicId=V10ROGNG1pvnCX_NR8AVjZiQ2dBeE5qSXlOalV6TnpNeVV6KkNYNFpXc3lvIQcAcGhvdG9neg!!_1606565714544000_4&pre=http%3A%2F%2Fa1.qpic.cn%2Fpsc%3F%2FV10ROGNG1pvnCX%2FruAMsa53pVQWN7FLK88i5rFe4fjq*ZHtlzkaEJTN7*QlCGz0Q3gr9TA6bfoyVg2WHAnERhVTeJj58tqqx*EvO5y8dtDHdJn8fSUsg1aGYZQ!%2Fm%26ek%3D1%26kp%3D1%26pt%3D0%26bo%3D0gNdAtIDXQIDGTw!%26tl%3D1%26vuin%3D1622653732%26tm%3D1606564800%26sce%3D60-3-3%26rf%3D0-0&useqzfl=1&useinterface=1&noCloseBtn=0&inqq=1
你的问题是什么?
到while temp:执行下面的语句时,为什么先执行 temp = temp // 10,然后将得到的新temp带入sum += (temp % 10) ** 3,这步没看懂 苏东坡大居士 发表于 2020-11-28 20:18
到while temp:执行下面的语句时,为什么先执行 temp = temp // 10,然后将得到的新temp带入sum += (temp % ...
for i in range(100, 1000):#遍历100到1000所有整数
sum = 0#定义求和变量,初值为0
temp = i
while temp:#当i得值不等于0时,一直循环
sum = sum + (temp%10) ** 3 #temp取余10得到最后一位的数值,sum等于temp各位数之和
temp //= 10 # 注意这里要使用地板除 这里得到temp的前两位
if sum == i: #判断i是否等于其各位数的三次方之和
print(i)#打印满足情况的i得值 Twilight6 发表于 2020-11-28 20:18
你的问题是什么?
到while temp:执行下面的语句时,为什么先执行 temp = temp // 10,然后将得到的新temp带入sum += (temp % 10) ** 3,这步没看懂 苏东坡大居士 发表于 2020-11-28 20:20
到while temp:执行下面的语句时,为什么先执行 temp = temp // 10,然后将得到的新temp带入sum += (temp ...
善用论坛的搜索功能~,里面有注释解析:
[已解决] 第9课水仙花数问题
https://fishc.com.cn/thread-185547-1-1.html
(出处: 鱼C论坛)
昨非 发表于 2020-11-28 20:19
谢谢大兄嘚,抱拳 苏东坡大居士 发表于 2020-11-28 20:26
谢谢大兄嘚,抱拳
小事儿 Twilight6 发表于 2020-11-28 20:21
善用论坛的搜索功能~,里面有注释解析:
[已解决]第9课水仙花数问题
懂了,python运算符不是太熟悉,%是取模运算,返回余数,而100%10没有余数,并不是跳过了。刚刚试过了使用365输入,sum打印出来就是125
页:
[1]