当前位置:网站首页>队列解决约瑟夫问题
队列解决约瑟夫问题
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
边栏推荐
- [leetcode 383] ransom letter
- [leetcode 459] duplicate substring
- Sakura substring thinking
- Cf515b drazil and his happy friends
- Framework analysis 2 Source code - login authentication
- Fact final variable and final variable
- ThreadLocal. Threadlocalmap analysis
- 自动控制原理知识点整合归纳(韩敏版)
- [leetcode 67] sum of two binary numbers
- Problems and solutions of database migration
猜你喜欢
卡尔曼滤波与惯性组合导航
检测技术与原理
Paper on LDCT image reconstruction: edge enhancement based transformer for medical image denoising
Contrôle automatique (version Han min)
[leetcode 59] spiral matrix II
[leetcode 401] binary Watch
[leetcode 67] sum of two binary numbers
lambda expressions
Chapter 3 of linear algebra - Elementary Transformation of matrix and system of linear equations
Advanced operation of idea debug
随机推荐
Delete and truncate
[leetcode 954] double pair array
Collection and map thread safety problem solving
Paper on Image Restoration - [red net, nips16] image restoration using very deep revolutionary encoder decoder networks wi
C # Foundation
Create binary tree
几行代码教你爬取LOL皮肤图片
List segmentation best practices
Kalman filter and inertial integrated navigation
Best practices for MySQL storage time
RPC must know and know
Reading of denoising papers - [cvpr2022] blind2blind: self supervised image denoising with visible blind spots
Example of ticket selling with reentrant lock
Framework analysis 1 Introduction to system architecture
Import of data
檢測技術與原理
What is the difference between the basic feasible solution and the basic feasible solution in linear programming?
Definition of C class and method
Export of data
MySQL table constraints and table design