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. Kafka剖析(一):Kafka背景及架构介绍

    http://www.infoq.com/cn/articles/kafka-analysis-part-1/ Kafka是由LinkedIn开发的一个分布式的消息系统,使用Scala编写,它以可水平 ...

  2. [LintCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  3. [LintCode] Swap Two Nodes in Linked List 交换链表中的两个结点

    Given a linked list and two values v1 and v2. Swap the two nodes in the linked list with values v1 a ...

  4. Linux_scp命令

    一.简单概念  scp是有Security的文件copy , 基于ssh登录 二.使用 1. 发送(本地--->远程服务器) [liuwl@hadoop09-linux-02 hadoop-2. ...

  5. Oracle中建表和指定表空间

    --建一个表create table HH2( tid number primary key ,--主键设定 tname varchar2(20) ); --删除表drop table HH; --表 ...

  6. js变量

    由于undefined和null两个值的比较是相等的,所以,未初始化的变量和赋值为null的变量会相等.这时,可以采用typeof变量的类型进行比较.但,建议还是养成编码的规范,不要忘记初始化变量. ...

  7. Bootstrap (导航、标签、面包屑导航)

    导航 Bootstrap中可用的导航有相似的标记,用基类.nav开头,这是相似的部分.改变修饰类可以改变样式. <!DOCTYPE html> <html> <head& ...

  8. SQL Server 触发器(转)

    触发器是一种特殊类型的存储过程,它不同于之前的我们介绍的存储过程.触发器主要是通过事件进行触发被自动调用执行的.而存储过程可以通过存储过程的名称被调用. Ø 什么是触发器 触发器对表进行插入.更新.删 ...

  9. BZOJ4443:[SCO2015]小凸玩矩阵

    题目大意:给一个N*M的矩阵,选出N个数,使得每行没列只选一个数,求第K大的数的最小值是多少? 二分答案,第k大的数<=x,则有N-k+1个数<=k,用二分图判定. #include< ...

  10. VirtualMachine所支持的操作

    在JDK中com.sun.tools.attach.VirtualMachine提供了一些从外部进程attach到jvm上,并执行一些操作的功能.VirtualMachine的子类HotSpotVir ...