百度百科:http://baike.baidu.com/link?url=msOmvPMbbYAhxiaRjsBLi1WRRFL1WuDXZXdlrQE-vOvnslpORlOP_4_hgNWgH5DU
约瑟夫问题:
问题描述:n个人(编号0~(n-1)),从0开始报数,报到(m-1)的退出,剩下的人继续从0开始报数。求胜利者的编号。
我们知道第一个人(编号一定是(m-1) mod n) 出列之后,剩下的n-1个人组成了一个新的约瑟夫环(以编号为k=m mod n的人开始):
k k+1 k+2 … n-2,n-1,0,1,2,… k-2
并且从k开始报0。
我们把他们的编号做一下转换:
k –> 0
k+1 –> 1
k+2 –> 2


k-2 –> n-2
变换后就完完全全成为了(n-1)个人报数的子问题,假如我们知道这个子问题的解:例如x是最终的胜利者,那么根据上面这个表把这个x变回去不刚好就是n个人情况的解吗?!!变回去的公式很简单,相信大家都可以推出来:x’=(x+k) mod n
如何知道(n-1)个人报数的问题的解?对,只要知道(n-2)个人的解就行了。(n-2)个人的解呢?当然是先求(n-3)的情况 —- 这显然就是一个倒推问题!好了,思路出来了,下面写递推公式:
令f表示i个人玩游戏报m退出最后胜利者的编号,最后的结果自然是f[n]
递推公式
f[1]=0;
f=(f+m) mod i; (i>1)
有了这个公式,我们要做的就是从1-n顺序算出f的数值,最后结果是f[n]。因为实际生活中编号总是从1开始,我们输出f[n]+1
由于是逐级递推,不需要保存每个f,程序也是异常简单:
Joseph
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 50099 Accepted: 19031
Description

The Joseph’s problem is notoriously known. For those who are not familiar with the original problem: from among n people, numbered 1, 2, …, n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved.

Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy.

Input

The input file consists of separate lines containing k. The last line in the input file contains 0. You can suppose that 0 < k < 14.
Output

The output file will consist of separate lines containing m corresponding to k in the input file.
Sample Input

3
4
0
Sample Output

5
30

Source

Central Europe 1995
题意:比较有意思 有k个好蛋 k个坏蛋 要坏蛋都比好蛋先死完的情况下循环数是多少(m)
思路:数学模拟Joseph 如果好蛋破了 重来 m++;╮(╯▽╰)╭ 当坏蛋都是笨蛋吗?

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int a[15]={0};//标记
int main ()
{int k;while(~scanf("%d",&k),k){if(a[k]){printf("%d\n",a[k]);continue;}int n = 2*k;int f[50]={0};int m = 1;for(int i=1;i<=k;i++){f[i] = (f[i-1] + m-1) % (n-i+1);//(n-i+1)为剩下的人数if(f[i]<k){m++;i=0;}}a[k] = m;printf("%d\n",m);}
}

Eeny Meeny Moo
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3295 Accepted: 2283
Description

Surely you have made the experience that when too many people use the Internet simultaneously, the net becomes very, very slow.
To put an end to this problem, the University of Ulm has developed a contingency scheme for times of peak load to cut off net access for some cities of the country in a systematic, totally fair manner. Germany’s cities were enumerated randomly from 1 to n. Freiburg was number 1, Ulm was number 2, Karlsruhe was number 3, and so on in a purely random order.
Then a number m would be picked at random, and Internet access would first be cut off in city 1 (clearly the fairest starting point) and then in every mth city after that, wrapping around to 1 after n, and ignoring cities already cut off. For example, if n=17 and m=5, net access would be cut off to the cities in the order [1,6,11,16,5,12,2,9,17,10,4,15,14,3,8,13,7]. The problem is that it is clearly fairest to cut off Ulm last (after all, this is where the best programmers come from), so for a given n, the random number m needs to be carefully chosen so that city 2 is the last city selected.

Your job is to write a program that will read in a number of cities n and then determine the smallest integer m that will ensure that Ulm can surf the net while the rest of the country is cut off.
Input

The input will contain one or more lines, each line containing one integer n with 3 <= n < 150, representing the number of cities in the country.
Input is terminated by a value of zero (0) for n.
Output

For each line of the input, print one line containing the integer m fulfilling the requirement specified above.
Sample Input

3
4
5
6
7
8
9
10
11
12
0
Sample Output

2
5
2
4
3
11
2
3
8
16
Source

Ulm Local 1996
题意:第一个人必须报数 所以就可以看成满足第一个(二号)为win的条件下 m为多少的问题

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int Jos(int n,int m)//约瑟夫环
{int s = 0;for(int i=2;i<=n;i++)s = (s+m) % i;return s;
}
int main()
{int n;while(~scanf("%d",&n),n){int i = 1;while(Jos(n-1,i) != 0)//因为第一个必须kill 所以看做n-1的环i++;printf("%d\n",i);}
}

约瑟夫环问题 poj 1012 poj 2244相关推荐

  1. POJ 3517 And Then There Was One( 约瑟夫环模板 )

    链接:传送门 题意:典型约瑟夫环问题 约瑟夫环模板题:n个人( 编号 1-n )在一个圆上,先去掉第m个人,然后从m+1开始报1,报到k的人退出,剩下的人继续从1开始报数,求最后剩的人编号 /**** ...

  2. POJ 1012 Joseph 题解

    POJ 1012 Joseph 题解 题目解读: The Joseph's problem is notoriously known. For those who are not familiar w ...

  3. HDU 5643 King's Game 【约瑟夫环】

    题意: 变形的约瑟夫环,最初为每个人编号1到n,第i次删去报号为i的人,然后从它的下一个人开始重新从1开始报号,问最终剩下第几号人? 分析: 首先看一下裸的约瑟夫环问题: 共n个人,从1开始报数,报到 ...

  4. python中约瑟夫环程序_Python实现约瑟夫环问题的方法

    本文实例讲述了Python实现约瑟夫环问题的方法.分享给大家供大家参考,具体如下: 题目:0,1,...,n-1这n个数字排成一个圆圈,从数字0开始每次从这个圆圈里删除第m个数字.求出这个圆圈里剩下的 ...

  5. 约瑟夫环问题的两种解法(详解)

    约瑟夫环问题的两种解法(详解) 题目: Josephus有过的故事:39 个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人抓.于是决定了自杀方式,41个人排成一个圆 ...

  6. python约瑟夫环问题给十个学生编号报到3者出列_趣味算法--约瑟夫环问题(示例代码)...

    问题描述 已知n个人(以编号1,2,3,...,n分别表示)围坐在一张圆桌上.指定编号为k的人开始从1报数,数到m的那个人出列:出列那个人的下一位又从1开始报数,数到m的那个人出列:以此规则重复下去, ...

  7. 一文读懂约瑟夫环算法

    2020-05-25 20:13:40 作者 | 扬帆向海 责编 | 王晓曼 出品 | CSDN博客 问题描述 约瑟夫问题(有时也称为约瑟夫斯置换,是一个出现在计算机科学和数学中的问题.在计算机编程的 ...

  8. 面试题小记:1、统计字符串出现的次数,2、约瑟夫环问题

    今天面到了一个比较有意思的笔试题,先记录一下: 1.字符串类似'aaabbccddd',写个方法得出'3a2b2c3d',即统计字符串出现的个数 $arr = str_split('aaabbccdd ...

  9. java实现简单的约瑟夫环问题(二)

    Josephus(约瑟夫)问题的数学方法 前面的内容都是直接来来自于百度百科,后面才是我对这段话的理解 无论是用链表实现还是用数组实现都有一个共同点:要模拟整个游戏过程,不仅程序写起来比较烦,而且时间 ...

  10. 约瑟夫环双向链表c语言实,双向链表与约瑟夫环代码

    双向链表 //注意:该文件操作的链表为带头结点双向链表,头结点数据为-1 #include #include #include #define OK 1 #define ERROR 0 typedef ...

最新文章

  1. 径向基函数神经网络_基于RBF神经网络的网络安全态势感知预测研究
  2. 花5分钟看这篇之前,你才发现你不懂RESTful
  3. python计算生态的命名_Python计算生态之random库
  4. Django View使用装饰器捕获数据库连接异常
  5. mongodb连接失败_MongoDB 基础入门
  6. 软核、硬核以及固核的概念
  7. Objective-C设计模式——单例Singleton(对象创建)
  8. 李洋疯狂C语言之关于自增自减遇到的一些问题
  9. 10外置面板没声音_现在电脑机箱只能用外置光驱了?看我装了个隐蔽式光驱
  10. 淘宝现重大BUG,是程序员报复?官方回应
  11. “酸碱体质理论”是个骗局
  12. 关于sizeof(struct student)的问题
  13. 浅谈BeanDefinition、BeanDefinitionMap、RootBeanDefintion三者的关系
  14. abaqus如何并行计算_Abaqus软件与并行计算的硬件配置
  15. android httpclient版本,Android studio使用http 没有 HttpClient
  16. ThreeJS 实现等值线效果
  17. 数字PCR的特点、优势和局限性浅析
  18. 利用windbg简单调试dump文件
  19. 第十一章 枚举与泛型 总结
  20. 写代码累了,读读这些书~

热门文章

  1. iOS开发:2分钟快速集成支付宝快捷支付
  2. 当我们点击一个文本域在IE下会发生的事件
  3. 不同方式遍历Map集合
  4. PHP FCKeditor2.5 解决上传图片问题
  5. getElementsByName和getElementByID
  6. Unity - Windows获取屏幕分辨率、可用区域
  7. Androidstudio代码自动提示不出来解决方法
  8. JavaWeb9大内置对象的作用与作用域
  9. mysql同步row模式_mysql行模式(ROW)主从同步测试及错误修复
  10. php添加开机启动脚本_php-fpm开机自动启动Shell脚本