|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
def numc():
result = []
for each in range(100,1000):
if (each%10)**3+(each%100)**3+(each%1000)**3==each:
result.append(each)
return result
这个函数用来讯息找水仙花数的问题出在哪里呢
本帖最后由 sunrise085 于 2020-8-4 15:14 编辑
你的if语句的第一项是个位,第二项不是十位,第三项也不是百位
- def numc():
- result = []
- for each in range(100,1000):
- if (each%10)**3+(int(each%100/10))**3+(int(each/100))**3==each:
- result.append(each)
- return result
复制代码
还可以先转字符串,再转回数字
- def numc():
- result = []
- for each in range(100,1000):
- if sum(int(i)**3 for i in str(each))==each:
- result.append(each)
- return result
复制代码
|
|