100只猴子选猴王,100只猴子围成一圈,从一开始,每到第三只就出圈,紧邻的下一只继续,直到最后一只。

img

能看一下那三条注释下我的代码有什么问题,就是运行不了,太难受了,感谢帮助

n = 100
lst = [1] * n
pos = 0
cnt = 0
total = 0 
while True: 
    while lst[pos] == 0:
        pos = (pos + 1) % n
    total += 1
    if cnt == n - 1:
        break
    if total % 3 == 0:
        lst[pos] = 0
        cnt = cnt + 1
    pos = (pos + 1) % n
print(lst.index(1) + 1)

n = 100
lst = [1] * n
pos = 0
cnt = 0
total = 0 
while True: 
    while lst[pos] == 0:
        pos = (pos + 1) % n
    total += 1
    if cnt == n - 1:
        print(lst.index(1) + 1)
        break
    if total % 3 == 0:
        lst[pos] = 0
        cnt += 1
    pos = (pos + 1) % n