鱼C论坛

 找回密码
 立即注册
查看: 3138|回复: 3

[已解决]课后作业求阶梯数的问题

[复制链接]
发表于 2023-3-8 18:52:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
steps = 7
i = 1
FIND = False

while i < 100:
    if (steps % 7 == 0) and (steps % 6 == 5) and (steps % 5 == 4) and(steps % 3 == 2) and (steps % 2 == 1) :
        FIND = True
        break
        
    i = i + 1

if FIND == True:
    print('阶梯数是:', steps)
else:
    print('在程序限定的范围内找不到答案!')

为什么把steps能被7整除写成(steps % 7 == 0) 并且写到 if 里面不行,要写成下面这样才行?
为什么把steps能被7整除写成(steps % 7 == 0) 并且写到 if 里面不行,要写成下面这样才行?

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
        break
    else:
        steps = 7 * (i + 1)
    i = i + 1

if FIND == True:
    print('阶梯数是:', steps)
else:
    print('在程序限定的范围内找不到答案!')
最佳答案
2023-3-8 19:17:35
本帖最后由 chinajz 于 2023-3-8 19:20 编辑

理解完全是对的,只是7的倍数效率似乎更好些。
  1. #steps = 7
  2. i = 7
  3. FIND = False

  4. while i < 120:
  5.     if (i % 7 == 0) and (i % 6 == 5) and (i % 5 == 4) and(i % 3 == 2) and (i % 2 == 1) :
  6.         FIND = True
  7.         break
  8.         
  9.     i = i + 7

  10. if FIND == True:
  11.     print('阶梯数是:',i)
  12. else:
  13.     print('在程序限定的范围内找不到答案!')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-3-8 19:13:21 | 显示全部楼层
本帖最后由 chinajz 于 2023-3-8 19:26 编辑

修改一下,也是可以的:
  1. steps = 7
  2. i = 1
  3. FIND = False

  4. while i < 100:
  5.     steps = i*7
  6.     if (steps % 7 == 0) and (steps % 6 == 5) and (steps % 5 == 4) and(steps % 3 == 2) and (steps % 2 == 1) :
  7.         FIND = True
  8.         break
  9.         
  10.     i = i + 1

  11. if FIND == True:
  12.     print('阶梯数是:', steps)
  13. else:
  14.     print('在程序限定的范围内找不到答案!')
复制代码

或者:
  1. #steps = 7
  2. i = 1
  3. FIND = False

  4. while i < 120:
  5.     if (i % 7 == 0) and (i % 6 == 5) and (i % 5 == 4) and(i % 3 == 2) and (i % 2 == 1) :
  6.         FIND = True
  7.         break
  8.         
  9.     i = i + 1

  10. if FIND == True:
  11.     print('阶梯数是:',i)
  12. else:
  13.     print('在程序限定的范围内找不到答案!')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-3-8 19:17:35 | 显示全部楼层    本楼为最佳答案   
本帖最后由 chinajz 于 2023-3-8 19:20 编辑

理解完全是对的,只是7的倍数效率似乎更好些。
  1. #steps = 7
  2. i = 7
  3. FIND = False

  4. while i < 120:
  5.     if (i % 7 == 0) and (i % 6 == 5) and (i % 5 == 4) and(i % 3 == 2) and (i % 2 == 1) :
  6.         FIND = True
  7.         break
  8.         
  9.     i = i + 7

  10. if FIND == True:
  11.     print('阶梯数是:',i)
  12. else:
  13.     print('在程序限定的范围内找不到答案!')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-3-10 10:40:28 | 显示全部楼层
  1. def stepNumbers(a,b):
  2.     stepNumbers = []
  3.     if a > b:
  4.         a, b = b, a
  5.     k = a//7
  6.     for i in range(k*7, b, 7):
  7.         if i%2 == 1 and i%3 == 2 and i%4 == 3 and i%5 == 4 and i%6 == 5:
  8.             stepNumbers.append(i)
  9.     if stepNumbers == []:
  10.         print(f'[{a},{b}]之间没有阶梯数。')
  11.     else:
  12.         print(f'[{a},{b}]之间的阶梯数有{stepNumbers}')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-24 21:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表