我的朋友告诉我约瑟夫斯问题,你有很多41
人坐在圈子里。人号1
有剑,杀右边人,将剑传给下一个人。这种情况一直持续到只剩下一个人活着。我在python中提出了这个解决方案:
print('''There are n people in the circle. You give the knife to one of
them, he stabs person on the right and
gives the knife to the next person. What will be the number of whoever
will be left alive?''')
pplList = []
numOfPeople = int(input('How many people are there in the circle?'))
for i in range(1, (numOfPeople + 1)):
pplList.append(i)
print(pplList)
while len(pplList) > 1:
for i in pplList:
if i % 2 == 0:
del pplList[::i]
print(f'The number of person which survived is {pplList[0]+1}')
break
但它只对42
人有效。我应该怎么做,或者我应该如何更改代码,以便它适用于例如100, 1000
圈子中的更多人?
我查看了约瑟夫斯问题并看到了不同的解决方案,但我很好奇我的答案在经过一些小的调整后是否正确,或者我应该从头开始。