当前位置:网站首页>约瑟夫问题
约瑟夫问题
2022-06-13 04:38:00 【算法与编程之美】
1 问题
已知 n 个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。.从编号为 k 的人开始报数,数到 m 的那个人出圈;他的下一个人又从 1 开始报数,数到 m 的那个人又出圈;依此规律重复下去,直到剩余最后一个胜利者。. 例如:有10个人围成一圈进行此游戏,每个人编号为 1-10 。. 若规定数到 3 的人出圈。. 则游戏过程如下。(1)开始报数,第一个数到 3 的人为 3 号,3 号出圈。(2)从4号重新从1开始计数,则接下来数到3的人为6号,6号出圈。(3)按以上的方法依次类推。
2 方法
3 实验结果与讨论
通过实验、实践等证明提出的方法是有效的,是能够解决开头提出的问题。
代码清单 1
ls = [I for I in range (1,42)] ls = [] num = 0 while len(ls1) > 1: num += 1 count = ls1.pop(0) if num == 3: ls2.append(count) num = 0 else: ls1.append(count) print(ls1) |
4 结语
本文介绍了约瑟夫环的问题来历,以及如何使用Python设计程序解决约瑟夫环,并且进行了拓展,使该程序能应用于更多相似的问题。但对于使用到函数的介绍相对空乏,并未通过举例详细介绍函数的使用方法,会对此加以改进。后续还会对Python算法的经典案例进行研究并以自己认为容易理解的方式进行分析,敬请期待!
边栏推荐
- 2022年建筑架子工(建筑特殊工种)特种作业证考试题库及在线模拟考试
- Tree array explanation
- Collection of wrong questions in soft test -- morning questions in the first half of 2010
- ES6 learning
- Develop go using vscode
- rust编程-链表:使用struct实现链表,使用堆合并k个升序链表,自定义Display
- Nodejs parsing get request URL string
- Mysql database installation
- Applet version update
- Ionic Cordova command line
猜你喜欢
Collection of wrong questions in soft test -- morning questions in the first half of 2010
Converting MySQL data to PostgreSQL with Navicat
用navicat将mysql数据转化为postgresql
Read paper 20 together: spatiotemporal prediction of PM2.5 concentration by idw-blstm under different time granularity
如何只用4步,实现一个自定义JDBC驱动?
【Try to Hack】upload-labs通关(暂时写到12关)
Ctfshow SQL injection (231-253)
CTFSHOW SQL注入篇(231-253)
Explanation of line segment tree
Small program imitating Taobao Jiugong grid sliding effect
随机推荐
Small program input element moving up
E - Lucky Numbers
[sword finger offer] interview question 24 Reverse linked list
Swiper plug-in
Read paper 20 together: spatiotemporal prediction of PM2.5 concentration by idw-blstm under different time granularity
php开发14 友情链接模块的编写
February 25, 2021 (Archaeology 12 year Landbridge provincial competition)
CTFSHOW SQL注入篇(211-230)
Li Kou brush question 647 Palindrome substring
【JS解决】leedcode 117. 填充每个节点的下一个右侧节点指针 II
Ctfshow SQL injection (211-230)
Converting MySQL data to PostgreSQL with Navicat
2022道路运输企业安全生产管理人员操作证考试题库及答案
【Try to Hack】upload-labs通关(暂时写到12关)
Redis
个人总结的MVP框架
Implementation of homepage header function in PHP development blog system
Notes on software test for programmers -- basic knowledge of software development, operation and maintenance
Redis data persistence
Tita: Xinrui group uses one-to-one talk to promote the success of performance change