|  | 
 
| 
第六课时
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  爱因斯坦的难题
 爱因斯坦曾出过这样一道有趣的数学题:有一个长阶梯,若每步上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)
 | 
 |