关于python课程中爱因斯坦问题的求助,我真的是超级小白
第六课时爱因斯坦的难题
爱因斯坦曾出过这样一道有趣的数学题:有一个长阶梯,若每步上2阶,最后剩1阶;若每步上3阶,最后剩2阶;若每步上5阶,最后剩4阶;若每步上6阶,最后剩5阶;只有每步上7阶,最后刚好一阶也不剩。
以下是正确代码
x = 7
i = 1
flag = 0
while i <= 100:
if (x%2 == 1) and (x%3 == 2) and (x%5 == 4) and (x%6==5):
flag = 1
else:
x = 7 * (i+1) # 根据题意,x一定是7的整数倍,所以每次乘以7
i += 1
if flag == 1:
print('阶梯数是:', x)
else:
print('在程序限定的范围内找不到答案!')
以下是我的代码,不知道为什么就是运行不了,快崩溃了,求大神解惑
whether=0
i=1
x=1
while i <= 100:
if (7*i)%2 == 1 and (7*i)%3 == 2 and (7*i)%5 == 4 and (7*i)%6 == 5:
x = 7*i
whether = 1
else:
i = i+1
if whether == 1:
print(x)
else:
print('you can\'t find the answer')
whether = 0
i = 1
x = 1
while i <= 100:
if (7 * i) % 2 == 1 and (7 * i) % 3 == 2 and (7 * i) % 5 == 4 and (7 * i) % 6 == 5:
x = 7 * i
whether = 1
print(x)
i = i + 1
if whether == 0:
print('you can\'t find the answer')
或者:
whether = 0
i = 1
x = 1
while i <= 100:
if (7 * i) % 2 == 1 and (7 * i) % 3 == 2 and (7 * i) % 5 == 4 and (7 * i) % 6 == 5:
x = 7 * i
whether = 1
break # 跳出循环
i = i + 1
if whether == 0:
print('you can\'t find the answer')
else:
print(x) 非常感谢版主的解答!还有几个小问题想问一下
1.whether在这里是否是作为标杆来表示是否能够找到答案,如果能就是1,不能就是0,如果是这样的话能否用true 或者 false 来进行赋值?
2.我的代码之所以不能运行是否是因为没有使用break,也就是跳出循环,所以导致哪怕找到了答案也会继续进行循环?
最后再次感谢版主的回答,鱼c社区太温暖了,199没白充好吧!
页:
[1]