使用python列表的pop方法求解约瑟夫环问题

约瑟夫环问题是数学和计算机科学中的一个经典问题,详见约瑟夫问题百度词条。之前在计算机二级考试中遇到过,当时用C语言写的,感觉有点复杂。现在学习python,发现可以用列表的pop方法完美模拟把人移除的操作,因此重新用python写了一下:

问题的具体描述:有34个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
python代码:

ring_list = [ i+1 for i in range(34)]
bias = 0
print(ring_list)

while(len(ring_list)>1):
	pop_time = (len(ring_list)+bias)//3
	residue = (len(ring_list)+bias)%3
	pop_index = [ i*3-bias+2 for i in range(pop_time)]
	pop_index.sort(reverse =True)
	for index in pop_index:
		ring_list.pop(index)
	print(ring_list)
	bias = residue

输出结果:

[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]
[1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34]
[1, 4, 5, 8, 10, 13, 14, 17, 19, 22, 23, 26, 28, 31, 32]
[1, 4, 8, 10, 14, 17, 22, 23, 28, 31]
[1, 4, 10, 14, 22, 23, 31]
[1, 10, 14, 23, 31]
[10, 14, 31]
[10, 31]
[10]
[Finished in 0.7s]

我们可以看到,最终留下的人为10号。

我们也可以把这段代码封装成函数,把34和3作为参数传入,也可得到同样的结果:

def joseph(total_num,pop_num):
	ring_list = [ i+1 for i in range(total_num)]
	bias = 0
	print(ring_list)
	while(len(ring_list)>1):
		pop_time = (len(ring_list)+bias)//pop_num
		residue = (len(ring_list)+bias)%pop_num
		pop_index = [ i*3-bias+2 for i in range(pop_time)]
		pop_index.sort(reverse =True)
		for index in pop_index:
			ring_list.pop(index)
		print(ring_list)
		bias = residue

joseph(34,3)
发布了179 篇原创文章 · 获赞 9 · 访问量 5551

猜你喜欢

转载自blog.csdn.net/weixin_41855010/article/details/104511830