当前位置:网站首页>队列解决约瑟夫问题
队列解决约瑟夫问题
2022-04-23 05:46:00 【圆滚滚的程序员】
队列 Queue 数据的添加发生在尾端,数据的移除发生在首端
仅有一个出口一个入口
First-in First-out, FIFO
实例,操作系统进程调度,I/O缓冲
class Queue:
def __init__(self):
self.items = []
def idEmpty(self):
return self.items == []
def enqueue(self,item):
self.items.insert(0,item)
def dequeue(self):
return self.items.pop()
def size(self):
return len(self.items)
约瑟夫问题
N个人围成一个圈, 从第一个人开始报数, 报到M的人出圈,
剩下的人继续从1开始报数, 报到M的人出圈;如此往复, 直到所有人出圈
def Joseph(nameList,num):
simqueue = Queue()
for name in nameList:
simqueue.enqueue(name)
while simqueue.size() > 1 :
for i in range(num):
simqueue.enqueue(simqueue.dequeue())
simqueue.dequeue()
return simqueue.dequeue()
print(Joseph(["赵二", "张三", "李四", "王五", "钱六", "孙七"], 7))
版权声明
本文为[圆滚滚的程序员]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_39483957/article/details/121598436
边栏推荐
- Explanation of login page
- -- SQL query and return limit rows
- Motor and drive (Qi Jinqing Edition)
- 8. Integer Decomposition
- SVN简单操作命令
- Chapter 3 of linear algebra - Elementary Transformation of matrix and system of linear equations
- MySQL occasional Caton
- Guaba and Computational Geometry
- Formation à la programmation
- Framework analysis 2 Source code - login authentication
猜你喜欢
Guaba and Computational Geometry
A general U-shaped transformer for image restoration
[leetcode 202] happy number
Reading of denoising paper - [ridnet, iccv19] real image denoising with feature attention
检测技术与原理
Generation of verification code
基于pygame库编写的五子棋游戏
Database - sorting data
Paper on Image Restoration - [red net, nips16] image restoration using very deep revolutionary encoder decoder networks wi
Substring Inversion (Easy Version)
随机推荐
The problem that the page will refresh automatically after clicking the submit button on the form is solved
Optional best practices
4. Print form
Chapter 3 of linear algebra - Elementary Transformation of matrix and system of linear equations
IO multiplexing of 09 redis
What is the difference between the basic feasible solution and the basic feasible solution in linear programming?
Best practices for MySQL storage time
Cf1427c the hard work of paparazzi
检测技术与原理
Explanation of login page
-- SQL query and return limit rows
Guaba and Computational Geometry
GDAL+OGR学习
C3p0 database connection pool usage
Kalman filter and inertial integrated navigation
[leetcode 290] word rules
MySQL table constraints and table design
Understanding and use of tp50, tp90 and tp99
Three ways to create threads
Algèbre linéaire chapitre 2 - matrice et son fonctionnement