已发布于同名公众号:车湾里
什么是约瑟夫环问题
约瑟夫问题 ,有时也称为约瑟夫斯置换,是一个出现在计算机科学和数学中的问题。在计算机编程的算法中,类似问题又称为约瑟夫环。又称“丢手绢问题”.
据说著名犹太历史学家 Josephus 有过以下的故事:在罗马人占领乔塔帕特后,39 个犹太人与 Josephus 及他的朋友躲到一个洞中,39 个犹太人决定宁愿死也不要被敌人抓到,于是决定了一个自杀方式,41 个人排成一个圆圈,由第 1 个人开始报数,每报数到第 3 人该人就必须自杀,然后再由下一个重新报数,直到所有人都自杀身亡为止。
Josephus 要他的朋友先假装遵从,他将朋友与自己安排在第 16 个与第 31 个位置,于是逃过了这场死亡游戏。
什么是队列
在数据结构中,队列是一种特殊的线性表,队列的特点是先进先出。
- 先进,表示队列的数据新增操作只能在末端进行,不允许在队列的中间某个结点后新增数据;
-
先出,队列的数据删除操作只能在始端进行,不允许在队列的中间某个结点后删除数据。
也就是说队列的增和删的操作只能分别在这个队列的队尾和队头进行。
用队列解决约瑟夫问题
运用队列数据结构的先进先出特点,可以很好地解决这个约瑟夫环问题;
41个人站成一个队伍,从队伍头部第一个出列,进行1,2,3 报数,如果是数到3,这个人就自裁,排他后面的一个人继续从 1 开始报数,…… 好刺激;
没有报数到 3 的人,回去站到队尾继续排队;按照如此规则,直到自裁完毕。
因为是数到 3 就自杀,那么 Josephus 想要作弊不自杀,则要保证他和他朋友是最后的两个人,好像是废话。
现在用 Python 代码来实现这个过程。
定义一个队列类
这个类包含了队列初始化、尾部插入元素(到队伍尾部排队)、头部弹出元素(从队伍头部出来报数)、计算队列长度、返回队列所有元素等几个函数
class Queue():
# 初始化一个空的列表
def __init__(self):
self.__list=[]
# 往队列里插入元素(尾部进)
def enqueue(self,item):
self.__list.append(item)
# 弹出队列里的元素(头部出)
def dequeue(self):
return self.__list.pop(0) # 弹出队列里最先进入的元素
# 计算队列的长度
def size(self):
return len(self.__list)
# 返回数组(便于显示过程)
def show(self):
return (self.__list)
定义函数实现约瑟夫问题
通过实现上面的 Queue 类,调用他的方法,实现约瑟夫问题
'''
namelist: 传入要自裁的清单
num:数到几自裁
r_size: 留下几个活口
'''
def josephus_problem(namelist,num,r_size):
m_q = Queue()
for name in namelist:
m_q.enqueue(name) # 把拿到的名字全部都放到队列里
print("初始的环",m_q.show())
# 队列的大小>留下的活口数量,就按照规则循环自裁
while m_q.size() > r_size:
for i in range(num-1):
# num-1个人,从队伍头部出列,排到队尾
m_q.enqueue(m_q.dequeue())
# 然后下一个人就自裁
next_person=m_q.dequeue()
# 输出谁自裁了,还剩下的环
print(next_person," 自裁了, 剩下:", m_q.show())
return m_q # 返回剩下的队列
调用上步定义的函数
'''
第一个参数,传入range(1,42) 代表1-41个人
第二个参数,传入3,代表数到 3 就自裁
第三个参数,传入2,代表留下 2 个活口
'''
print("还剩下:", josephus_problem(range(1,42), 3, 2).show())
得到结果
初始的环 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41]
3 自裁了, 剩下: [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 1, 2]
6 自裁了, 剩下: [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 1, 2, 4, 5]
9 自裁了, 剩下: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 1, 2, 4, 5, 7, 8]
12 自裁了, 剩下: [13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 1, 2, 4, 5, 7, 8, 10, 11]
15 自裁了, 剩下: [16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 1, 2, 4, 5, 7, 8, 10, 11, 13, 14]
18 自裁了, 剩下: [19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17]
21 自裁了, 剩下: [22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20]
24 自裁了, 剩下: [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23]
27 自裁了, 剩下: [28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26]
30 自裁了, 剩下: [31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29]
33 自裁了, 剩下: [34, 35, 36, 37, 38, 39, 40, 41, 1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32]
36 自裁了, 剩下: [37, 38, 39, 40, 41, 1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34, 35]
39 自裁了, 剩下: [40, 41, 1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34, 35, 37, 38]
1 自裁了, 剩下: [2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34, 35, 37, 38, 40, 41]
5 自裁了, 剩下: [7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34, 35, 37, 38, 40, 41, 2, 4]
10 自裁了, 剩下: [11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34, 35, 37, 38, 40, 41, 2, 4, 7, 8]
14 自裁了, 剩下: [16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34, 35, 37, 38, 40, 41, 2, 4, 7, 8, 11, 13]
19 自裁了, 剩下: [20, 22, 23, 25, 26, 28, 29, 31, 32, 34, 35, 37, 38, 40, 41, 2, 4, 7, 8, 11, 13, 16, 17]
23 自裁了, 剩下: [25, 26, 28, 29, 31, 32, 34, 35, 37, 38, 40, 41, 2, 4, 7, 8, 11, 13, 16, 17, 20, 22]
28 自裁了, 剩下: [29, 31, 32, 34, 35, 37, 38, 40, 41, 2, 4, 7, 8, 11, 13, 16, 17, 20, 22, 25, 26]
32 自裁了, 剩下: [34, 35, 37, 38, 40, 41, 2, 4, 7, 8, 11, 13, 16, 17, 20, 22, 25, 26, 29, 31]
37 自裁了, 剩下: [38, 40, 41, 2, 4, 7, 8, 11, 13, 16, 17, 20, 22, 25, 26, 29, 31, 34, 35]
41 自裁了, 剩下: [2, 4, 7, 8, 11, 13, 16, 17, 20, 22, 25, 26, 29, 31, 34, 35, 38, 40]
7 自裁了, 剩下: [8, 11, 13, 16, 17, 20, 22, 25, 26, 29, 31, 34, 35, 38, 40, 2, 4]
13 自裁了, 剩下: [16, 17, 20, 22, 25, 26, 29, 31, 34, 35, 38, 40, 2, 4, 8, 11]
20 自裁了, 剩下: [22, 25, 26, 29, 31, 34, 35, 38, 40, 2, 4, 8, 11, 16, 17]
26 自裁了, 剩下: [29, 31, 34, 35, 38, 40, 2, 4, 8, 11, 16, 17, 22, 25]
34 自裁了, 剩下: [35, 38, 40, 2, 4, 8, 11, 16, 17, 22, 25, 29, 31]
40 自裁了, 剩下: [2, 4, 8, 11, 16, 17, 22, 25, 29, 31, 35, 38]
8 自裁了, 剩下: [11, 16, 17, 22, 25, 29, 31, 35, 38, 2, 4]
17 自裁了, 剩下: [22, 25, 29, 31, 35, 38, 2, 4, 11, 16]
29 自裁了, 剩下: [31, 35, 38, 2, 4, 11, 16, 22, 25]
38 自裁了, 剩下: [2, 4, 11, 16, 22, 25, 31, 35]
11 自裁了, 剩下: [16, 22, 25, 31, 35, 2, 4]
25 自裁了, 剩下: [31, 35, 2, 4, 16, 22]
2 自裁了, 剩下: [4, 16, 22, 31, 35]
22 自裁了, 剩下: [31, 35, 4, 16]
4 自裁了, 剩下: [16, 31, 35]
35 自裁了, 剩下: [16, 31]
还剩下: [16, 31]
是在16 和 31 位置的人到最后才分别报数到3,所以啊,流氓不可怕,就怕流氓有文化啊。
另外 39 条性命还以为是上帝的安排呢,呜呼~
延伸的问题
30 个人在一条船上,超载,需要 15 人下船。于是人们排成一队,排队的位置即为他们的编号。报数,从 1 开始,数到 9 的人下船。如此循环,直到船上仅剩 15 人为止,问都有哪些编号的人下船了呢?
怎么计算,有了上面的方法,直接调用就好了:
'''
第一个参数,传入range(1,31) 代表1-30个人
第二个参数,传入9,代表数到9就下船
第三个参数,传入15,代表留下15个人
'''
josephus_problem(range(1,31), 9, 15).show()
感谢阅读~