lindalaputa 发表于 2021-9-9 07:01:38

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

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 s=1--> s=2--> s=3--> s=4--> s=5--> s=6-->

傻眼貓咪 发表于 2021-9-9 11:11:33

本帖最后由 傻眼貓咪 于 2021-9-9 11:16 编辑

students = 6 # 學生數量
lockers = # 儲物櫃狀態,0 表示關
print(*lockers) # 打印初始儲物櫃狀態

for n in range(1, students+1): # 第幾位學生
    for l in range(n-1, students, n): # 開/關 每第幾個儲物櫃
      lockers = int(not lockers) # 開變關,關變開
    print(*lockers) # 打印儲物櫃目前狀態0 0 0 0 0 0
1 1 1 1 1 1
1 0 1 0 1 0
1 0 0 0 1 1
1 0 0 1 1 1
1 0 0 1 0 1
1 0 0 1 0 0
页: [1]
查看完整版本: 求助,locker puzzle问题应该怎么做?