课后习题问题
爱因斯坦曾出过这样一道有趣的数学题:有一个长阶梯,若每步上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)
i += 1
if flag == 1:
print("阶梯数是",t)
else:
print("not found")
而且如何求出最少呢,这是我写的程序,但是没法求出最少,只能把符合条件的全部写列出来
t = 0
while t != -1:
if t % 2 == 1 and t % 3 == 2 and t % 5 == 4 and t % 6 == 5 and t % 7 == 0:
print(t,end = "")
print(" ",end = "")
t += 1
else:
t += 1
求出来一个以后就直接终止循环 t = 0
while t != -1:
if t % 2 == 1 and t % 3 == 2 and t % 5 == 4 and t % 6 == 5 and t % 7 == 0:
print(t)
break
else:
t += 1
退出循环可以用 break 语句
页:
[1]