The Josephus Problem

算法——The Josephus Problem 题目描述 The problem is named after Flavius Josephus, a Jewish historian who participated in and chronicled the Jewish revolt of 66-70C.E. against the Romans. Josephus, as a general, managed to hold the f... 阅读详情

      I recently find a class of questions called the josephus problem,all those questions is similar with the following:
    long long ago,there was a king,he is a mathematician but crue and brutall ,and he always invited many people to his palace,and let them sat around a table,and assigned everyone a number,assume there were 20 people,the numner is form 1 to 20.then the king counted them from nunber 1,when the total reached to 3,he will kill the person,and continue this from the next person ,and the result from 1,and when there were only 1 person, he will get 1 billion coins.if you were one of those people,which number you will sit to alive and get the 1 billion coins?
      This type of problem is called a "Josephus problem", after a story about a historian of the first century, Flavius Josephus, who survived the Jewish-Roman war perhaps due to his mathematical talents. In his book The Jewish Wars Flavius tells that he was one out of 41 Jewish rebels trapped by the Romans. His companions preferred suicide to escape, so they decided to draw lots to see who would kill whom so that they could avoid both capture and the sin of suicide. The idea that they decided to form a cycle and to kill every third person and to proceed around the circle until no one was left is probably a myth. According to the problem Josephus wasn't excited by the idea of killing himself, so he calculated where he has to stand to survive the vicious circle. The general Josephus problem; involves how many people will stand around a circle and how many shall be passed over before the next one is killed.
      We can consider this question in this way: if  there were only one person,he is the winner(he will get the 1 billion coins or not dead),his number is 1 in the circle,and we assume there were 2 people,the winner's number is 2 in this circle.
      Suppose there have n people,we list them beginning with 0 as the following:
      0 1 2 3 ....n-2 n-1 we assume the step is m which means we will kick the m person off.then his number must be (m-1)%n,and then left n-1 people,we also assume his number is k,and then the list is 0 1 2 ...k-1 k+1 ...n-2 ;we can change its order to k+1 k+2..1 2 ...n-2,now we assume again,the winner's number is x,What is the number of the winner in preceding cirle? I think this is very easy,the number equals to (k+x+1)%n,we know k=(m-1)%n,so the number of winner also equals to (x+m)%n.
      From preceding deducing,we can get the winner's number of  n people circle from n-1 people circle.and the 1 people circle winner's  is number 0 according to the preceding deducing.So this is a recursion:
the number we count is 3,which is also the m of the preceding paragraph.
total   index  number
1        0        1
2        1        2
3        1        2
4        0        1
5        3        4
      The programm in C++ programming language is the following:

#include<iostream.h>
void main()
{
    int total,step,i,result=0;
   cin>>total>>step;
    for(i=2;i<=total;i++)
    {
           result = (result+step)%i;
     }
     cout<<"the winner is:"<<result+1<<endl;
}

Date:2008-09-26

Place:Neusoft F3

josephus问题源代码 用c++解决了josephus问题:有n个人围在一个圆桌周围,现从第s个人开始报数,数到第m个人又出列…如此反复直到所有的人全部出列为只止。 立即下载

相关推荐

ESP32搭配FT6236触摸屏:从芯片手册到完整I2C驱动代码的保姆级解析

本文详细解析了ESP32搭配FT6236触摸屏的I2C驱动开发过程,从芯片手册解读到完整代码实现。通过硬件架构分析、寄存器配置、I2C通信封装及触摸数据处理等步骤,提供了一套完整的驱动解决方案,帮助开发者快速掌握ESP32与触摸屏的交互技术。

weixin_29174385的博客 301

SWUSTOJ 580: The World Population Explosion

一个字符占一个数组,存很多个字符串,需要用二维数组。

qq_52327660的博客 696

STM32Cubemx-H7-17-麦克纳姆轮驱动

我们先看.h,知道哪些函数会有哪些功能和宏定义参数首先 是在.h的宏定义处定义引脚编号,比如tb6612控制方向的引脚,以及设定了哪个定时器作为pwm输出,还有编码计次的中断引脚/* 硬件相关宏定义 *//*1 前进方向往上 42 3*/// TB6612 方向引脚定义/* 定时器相关宏定义 *//* 编码器相关宏定义 */把自己的引脚根据原理图填对应,方向引脚也是,如果发现反转就把对应的引脚定义反过来,编码也是,编码我们用一个引脚就好,都在cubemx配置好。

m0_74211379的博客 1432

The Josephus Problem - 约瑟夫环

Youtube上一个讲解约瑟夫环问题的视频非常棒,很形象,很具体。视频提供了两种解法。 第一种: n:是士兵的总数,a是不小于n情况下2的最大指数,l是剩余的士兵数,W(n)计算的就是士兵数为n的情况下最后幸存的士兵编号。 第二种: 二进制编码 士兵数为41,可以拆分为多个2的指数次方的和,然后将最高位移到最后,二进制计算得到的就是最后胜出的士兵编号。 视频地址,有兴趣的可以想办法看看...

probie的博客 681

基于图神经网络的社交网络虚假账号检测系统 (Graph-based Fake Account Detection System, GFADS)

FOLLOWSRETWEETSSHAREDCOMMENTSLIKESMENTIONSUseruser_idusernamejoin_datebioavatar_urllocationverifiedis_fakeLOAD CSV这是决定模型性能的关键环节,需结合和。XXedge_indexy新账号缺乏历史数据和丰富关系。全网图可能包含数亿节点和数十亿边,难以直接训练。社交网络是动态变化的,虚假账号策略也在不断进化。获取大规模、高质量的真实/虚假账号标注数据非常困难。

Deng945201314的博客 1485

STC15单片机实战项目软件开发第四讲 - 按键检测(状态机)

软件开发第四讲 - 按键检测(状态机) 一、工具链接 1、Keil c51 UV4 https://yunpan.360.cn/surl_yrIfYYmeRFk 2、STC-ISP下载软件 https://yunpan.360.cn/surl_yxH4CuUUvI3 二、学习板获取方式 方法1:自己设计PCB,自行打板焊接; 方法2:用群主设计的PCB,自行打板焊接; 方法3:向群主购买,淘宝链接...

硬件家园 3050

The Josephus problem

Legend has it that Josephus wouldn't have lived to become famous without his mathematical talents. During the Jewish-Roman war, he was among a band of 41 Jerish rebels trapped in a cave by Romans...

dengyue470288的博客 306

别再为OpenBCI_GUI安装发愁了!保姆级教程带你从Processing配置到成功运行(附常见错误解决)

本文提供了一份详细的OpenBCI_GUI安装教程,涵盖从Processing配置到成功运行的全过程,特别针对常见错误提供了解决方案。通过环境搭建、软件依赖安装、源码处理及首次运行实战,帮助用户高效完成数据可视化工具的部署与使用。

weixin_28366475的博客 275

SWUST OJ #580 The World Population Explosion C++实现

题目描述: In the past, when population grew, there was unexplored territory to inhabit. But now, almost all the habitable land has been explored. The world's population may reach 8.7 billion in 2033. It is clear that world population is a serious issue that n

m0_60531385的博客 1719

【LVGL】从HTML到LVGL:嵌入式UI的设计迁移与落地实践

嵌入式UI迁移实践:从HTML到LVGL 本文介绍了将HTML/CSS设计的UI原型迁移到嵌入式系统(LVGL)的完整流程,重点涉及"HTML→Figma→LVGL"的三步转换方法。具体内容包括: 设计迁移流程:通过Figma作为中间桥梁,将HTML原型转化为标准设计资产,再适配到LVGL库; 工具与参数准备:详细列出了设计迁移所需的工具链和需要提取的核心设计参数; Figma还原步骤:分组件讲解如何在Figma中1:1还原HTML设计,包括标题栏、压力显示区、图表区等核心组件; LVG

ymlmail的博客 1985

PoP(Package on Package)封装技术探讨

PoP(Package on Package)封装技术摘要 PoP是一种3D封装技术,通过垂直堆叠已完成封装的器件(如SoC+DRAM)实现高密度集成,典型应用于智能手机等空间受限场景。其核心优势包括:节省主板面积(垂直堆叠)、优化高速信号完整性(短互连路径)、提升供应链灵活性(独立测试与选型)。PoP结构分为底封装(通常为Flip-Chip BGA的SoC)和顶封装(如FBGA存储芯片),通过焊球阵列互连,一次回流焊完成组装。

sinat_15150363的博客 386

536: The Josephus Problem

题目描述 The problem is named after Flavius Josephus, a Jewish historian who participated in and chronicled the Jewish revolt of 66-70C.E. against the Romans. Josephus, as a general, managed to hold the f...

ZHappyDragon的博客 768

黑猫带你学eMMC协议第22篇:emmc可靠写详解(Write Reliability)

1 前言 1.1 声明 本文依据eMMC JEDEC5.1、网络资料及个人工作经验整理而成,如有部分错误请留言。 本文部分内容可能源自网络,侵删。 本文结合eMMC JEDEC5.1协议手册查看效果更佳。 1.2 内容提要 1、什么是可靠性写属性(Write Reliability)? 2、可靠写和普通的写有什么区别? 3、设置可靠写的流程及流程框图。 4、可靠写需要遵循哪些规则? 1.3 参考链接 本文主要参考链接:http://www.up48.com/cn/support/8_37.html 2 什么

黑猫奥利奥的博客 3128

SWUSTOJ #536 The Josephus Problem

The problem is named after Flavius Josephus, a Jewish historian who participated in and chronicled the Jewish revolt of 66-70C.E. against the Romans.

oafseoases 2111

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

SWUST oj 536: The Josephus Problem

题目描述 The problem is named after Flavius Josephus, a Jewish historian who participated in and chronicled the Jewish revolt of 66-70C.E. against the Romans. Josephus, as a general, managed to hold the f...

qq_44462054的博客 3480

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

swustoj-536 The Josephus Problem

The problem is named after Flavius Josephus, a Jewish historian who participated in and chronicled the Jewish revolt of 66-70C.E. against the Romans. Josephus, as a general, managed to hold the fortre...

Swust_ZzK的博客 1457

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

SWUST OJ 536: The Josephus Problem

The problem is named after Flavius Josephus, a Jewish historian who participated in and chronicled the Jewish revolt of 66-70C.E. against the Romans. Josephus, as a general, managed to hold the fortress of Jotapata for 47days, but after the fall of the cit

无奈清风吹过的博客 770

swust oj536: The Josephus Problem

题目描述 The problem is named after Flavius Josephus, a Jewish historian who participated in and chronicled the Jewish revolt of 66-70C.E. against the Romans. Josephus, as a general, managed to hold the fortress of Jotapata for 47days, but after the fall of t

qq_53746035的博客 713

swust.536 The Josephus Problem

作为将军,约瑟夫斯成功地坚守了约塔帕塔堡垒47天,但在城市沦陷后,他与另外40名坚决不投降的伙伴躲藏在一个附近的洞穴中。约瑟夫斯设法抽到了最后一个签,称为了洞穴中最后剩下的两个人之一,并说服了本应成为他受害者的那个人一起向罗马人投降。当然,还有更高明的方式(将十进制转换为二进制,利用左移的环位移来求解),后续我将二进制的方法运用弄清楚了之后,我将发布此题的另一种解法,下次见喽。也就是,一圈人中,从1开始报数,报到2的人被淘汰,由下一个人继续从1开始报数,直到最后有一位幸存者。

weixin_66829339的博客 321

约瑟夫问题 The Josephus Problem 非递归算法求解

据说著名犹太历史学家 Josephus有过以下的故事: 在罗马人占领乔塔帕特后,39 个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人抓到,于是决定了一个自杀方式,41个人排成一个圆圈,由第1个人开始报数,每报数到第3人该人就必须自杀,然后再由下一个重新报数,直到所有人都自杀身亡为止。然而Josephus 和他的朋友并不想遵从。首先从一个人开始,越过k-2个人

美丽世界的孤儿 2054
上一篇: System Properties of Java VM
下一篇: Microsoft Office Word、Excel and PowerPoint 2007 file format compatible package
tigerting
博客等级 码龄19年 12粉丝 26原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值