tengke 发表于 2022-5-9 14:53:59

python

steps = 7
i = 1
FIND = False
while i < 100:
    if (steps % 2 == 1) and (steps % 3 == 2) and (steps % 5 == 4) and (steps % 6 == 5):
      FIND = True
    else:
      steps = 7 * (i + 1)
    i = i + 1
if FIND == True:
    print('阶梯数是:', steps)
else:
    print('在程序限定的范围内找不到答案!')
这个是7的倍数问题我想显示在100倍数以内所有满足的数字和对应的7的倍数

Twilight6 发表于 2022-5-9 15:02:04


这是爱因斯坦阶梯问题,参考代码:

steps = 7
i = 1
while i < 100:
    if (steps % 2 == 1) and (steps % 3 == 2) and (steps % 5 == 4) and (steps % 6 == 5):
      print('阶梯数是:', steps)
    steps = 7 * (i + 1)
    i = i + 1

还可以这样改:
steps = 7
print("阶梯数是:", end="")
while steps < 1000:
    if (steps % 2 == 1) and (steps % 3 == 2) and (steps % 5 == 4) and (steps % 6 == 5):
      print(steps, end=" ")
    steps = steps + 7

isdkz 发表于 2022-5-9 15:04:20

steps = 7
i = 1
while i < 100:
    if (steps % 2 == 1) and (steps % 3 == 2) and (steps % 5 == 4) and (steps % 6 == 5):
      print(f'满足条件的数字:{i},对应的7的倍数:{steps}')
    else:
      steps = 7 * (i + 1)
    i = i + 1
页: [1]
查看完整版本: python