有n个人围成一圈,从0到n-1按顺序编号,从第一个人开始从1到k报数,报到k的人推出圈子,然后圈子缩小,从下个人继续游戏,问最后留下来的是几号?
from itertools import cycle
def demo(people, k): # 报数报到k的人退出
lst1 = people[:]
while len(lst1) > 1:
c = cycle(lst1) #将列表设为循环列表
for i in range(k):
t = next(c)
index = lst1.index(t)
lst1 = lst1[index + 1:] + lst1[:index]
return lst1[0]
people = list(range(1, 11)) #生成11个人
print(demo(people, 1))