Joseph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1512    Accepted Submission(s):
948

Problem 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
 
这是一道关于约瑟夫环的问题,题目的大意是总共有2k个伙计围成一圈,1~k是好人,k+1~2k是坏人,报数m,使得k+1~2k的坏人必须在好人之前出去,求出这个m。
 
很显然,这道题要模拟整个过程,因为题目要求k小于14,所以定义一个一位数组保存所求出的m值。
 
 #include <iostream>
using namespace std;
int Joseph(int k,int m)
{
int len = *k;
int i;
int n = ; //n是要出去的位置,起始位置初始化为1
for (i=;i<=k;i++)
{
n = (n+m-)%len; //第i次出去的人
if(n==) //最后一个人出去
n = len;
if(n<=k) //n小于或等于k时候,返回false
return false;
len--; //出去一个人,则长度减1
}
return true;
} int main()
{
int a[];
int i,j;
for (i=;i<;i++)
for(j=i+;;j++)
if (Joseph(i,j))
{
a[i] = j;
break;
}
int n;
while(cin>>n && n)
cout<<a[n]<<endl;
return ;
}

Hdu 1443 Joseph的更多相关文章

  1. poj 1012 &amp; hdu 1443 Joseph(约瑟夫环变形)

    题目链接: POJ  1012: id=1012">http://poj.org/problem?id=1012 HDU 1443: pid=1443">http:// ...

  2. hdu 1443 Joseph (约瑟夫环)

    Joseph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  3. hdu 1443 Joseph【约瑟夫环】

    题目 题意:一共有2k个人,分别为k个好人和k个坏人,现在我们需要每隔m个人把坏人挑出来,但是条件是最后一个坏人挑出来前不能有好人被挑出来..问最小的m是多少 约瑟夫环问题,通常解决这类问题时我们把编 ...

  4. HDU题解索引

    HDU 1000 A + B Problem  I/O HDU 1001 Sum Problem  数学 HDU 1002 A + B Problem II  高精度加法 HDU 1003 Maxsu ...

  5. hdu Joseph

    #include <cstdio> #include <iostream> #include <algorithm> using namespace std; ]; ...

  6. HDU 5860 Death Sequence(死亡序列)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  7. HDU 5860 Death Sequence(递推)

    HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...

  8. HDU 5643 King's Game 打表

    King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...

  9. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

随机推荐

  1. java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries

    在已经搭建好的集群环境Centos6.6+Hadoop2.7+Hbase0.98+Spark1.3.1下,在Win7系统Intellij开发工具中调试Spark读取Hbase.运行直接报错: ? 1 ...

  2. 学习bootstrap心得

    Bootstrap 使用了一些 HTML5 元素和 CSS 属性.为了让这些正常工作,您需要使用 HTML5 文档类型(Doctype). 因此,请在使用 Bootstrap 项目的开头包含下面的代码 ...

  3. 快排java实现

    package sort; public class QuickSort { public static final int cutoff = 3; /** * insertion sort * * ...

  4. Pixar Shorts 皮克斯动画短片全集

    [原创短片](Theatrical Shorts)16部 <安德鲁和威利冒险记><顽皮跳跳灯><红色的梦><锡铁小兵><小雪人大行动>< ...

  5. intellij idea 插件 ideaVim

    像Eclipse一样,idea这个公认最好的javaIDE也有Vim插件. 安装方法 File>Settings>Plugins>Install JetBrains plugin.. ...

  6. JQuery类型转换

    来自:http://blog.csdn.net/kfanning/archive/2010/04/14/5485412.aspx 转换成数字 ECMAScript提供了两种把非数字的原始值转换成数字的 ...

  7. Thinkphp验证码异步验证第二次及以后验证,验证错误----待解决

    今天进行差错时遇到的问题.别人的回答----链接(http://www.thinkphp.cn/topic/28968.html) 3.2.3版本里的check方法会在第一次验证正确后清除SESSIO ...

  8. geometric median

    The geometric median of a discrete set of sample points in a Euclidean space is the point minimizing ...

  9. 成功熬了四年还没死?一个IT屌丝创业者的深刻反思

    三个IT屌丝创业的故事 从前有三个屌丝,聚在一起做网络.提供免费的网络服务,砸锅卖铁,通宵达旦,除了卖肾,啥都做了.3年后终于做到了五百万用户.对于年轻人来说,能把五百万人玩弄于鼓掌之间,已经是很牛逼 ...

  10. 虚拟机设置静态ip

    最近学习hadoop,用到虚拟机来做分布式,由于hadoop要配置slave节点的主机名,所以需要修改hosts文件的ip地址和主机名的映射关系. 但是虚拟机每次重启后,ip地址都会变 ,这样每次都得 ...