鱼C论坛

 找回密码
 立即注册
查看: 2370|回复: 1

求助,locker puzzle问题应该怎么做?

[复制链接]
发表于 2021-9-9 07:01:38 | 显示全部楼层 |阅读模式

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

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

x
A school has 100 lockers and 100 students. All lockers are closed on the first day of school. As the students enter, the first student, denoted S1, opens every locker. Then the second student, S2, begins with the second locker, denoted L2, and closes every other locker. Student S3 begins with the third locker and changes every third locker(closes it if it was open, and opens it if it was closed). Student S4 begins with locker L4 and changes every fourth locker. Student S5 starts with L5 and changes every fifth locker, and so on, until student S100 changes L100. print out a list of the open locker numbers. Let N be the number of lockers and students.
For example, initial[0,0,0,0,0,0] s=1--> [1,1,1,1,1,1] s=2--> [1,0,1,0,1,0] s=3--> [1,0,0,0,1,1] s=4--> [1,0,0,1,1,1] s=5--> [1,0,0,1,0,1] s=6--> [1,0,0,1,0,0]
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-9-9 11:11:33 | 显示全部楼层
本帖最后由 傻眼貓咪 于 2021-9-9 11:16 编辑
  1. students = 6 # 學生數量
  2. lockers = [0 for _ in range(students)] # 儲物櫃狀態,0 表示關
  3. print(*lockers) # 打印初始儲物櫃狀態

  4. for n in range(1, students+1): # 第幾位學生
  5.     for l in range(n-1, students, n): # 開/關 每第幾個儲物櫃
  6.         lockers[l] = int(not lockers[l]) # 開變關,關變開
  7.     print(*lockers) # 打印儲物櫃目前狀態
复制代码
  1. 0 0 0 0 0 0
  2. 1 1 1 1 1 1
  3. 1 0 1 0 1 0
  4. 1 0 0 0 1 1
  5. 1 0 0 1 1 1
  6. 1 0 0 1 0 1
  7. 1 0 0 1 0 0
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-18 21:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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