Python:密谋约瑟夫斯

时间:2018-04-08 15:09:52

标签: python josephus

我试图通过Python实现Josephus问题的一个解决方案。 By certain references I came to this code

def josephus(list, k):
    # pop automatically skips the dead guy
    i = k
    while len(list) > 1:
        print(list.pop(i)) # kill prisoner at i
        i = (i + k) % len(list)
    print('Survivor: ', list[0])

测试list = [1,2,3,4,5,6]和k = 1:

的代码
josephus([1,2,3,4,5,6],1)

控制台生成输出:

2
4
6
3
1
Survivor:  5

那么我怎么能绘制约瑟夫问题来显示哪一个参与者每轮死亡。最后,只剩下幸存者。我想这个实现可以遵循这种风格:

约瑟夫斯问题在n = 12的圆形图中:

enter image description here

0 个答案:

没有答案