题目链接:

POJ  1012: 

id=1012">http://poj.org/problem?id=1012

HDU 1443: 

pid=1443">http://acm.hdu.edu.cn/showproblem.php?

pid=1443

约瑟夫环(百度百科): 

fr=aladdin" target="_blank">http://baike.baidu.com/view/717633.htm?fr=aladdin

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

题意:

共同拥有 k 个坏人 k 个好人坐成一圈(总人数就为2*k)。前面的 k 个为好人(编号1~k),后面的 k 个为坏人(编号k+1~2k),

现有一个报数为 m ,从编号为 1 的人開始报数。报到 m 的人会自己主动的死去。

求当 m 为何值时。能够使在出现有好人死亡前,k 个坏人已经所有死掉?

注意: 当前一轮第 m 个人死后,下一轮的编号为1的人是前一轮编号为 m+1 的人。

若前一轮恰好是最后一个人死掉,则下一轮循环回到开头那个人报“1”

k比較小,直接暴力打表!

代码例如以下:

#include <cstdio>
int main()
{
int p[17], ans[17] = {0};
int m, k;
for(k = 1; k <= 14; k++)
{
int n = 2*k;//总人数
m = 1;
for(int i = 1; i <= k; i++)
{
//由于编号是从1開始的
ans[i] = (ans[i-1]+m-1)%(n-i+1);
if(ans[i] < k)//说明杀掉了好人,不符合题意
{
i = 0;
m++;//枚举
}
}
p[k] = m;
// printf("%d\n",p[k]);
}
// return 0;
int kk;
while(scanf("%d",&kk) && kk)
{
printf("%d\n",p[kk]);
}
return 0;
}

poj 1012 &amp; hdu 1443 Joseph(约瑟夫环变形)的更多相关文章

  1. hdu 1443 Joseph【约瑟夫环】

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

  2. HDU 3089 (快速约瑟夫环)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3089 题目大意:一共n人.从1号开始,每k个人T掉.问最后的人.n超大. 解题思路: 除去超大的n之 ...

  3. Hdu 1443 Joseph

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

  4. Poj 3517 And Then There Was One(约瑟夫环变形)

    简单说一下约瑟夫环:约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个 ...

  5. HDU 5643 King's Game | 约瑟夫环变形

    经典约瑟夫环 }; ; i<=n; i++) { f[i] = (f[i-] + k) % i; } 变形:k是变化的 #include <iostream> #include &l ...

  6. 【约瑟夫环变形】UVa 1394 - And Then There Was One

    首先看到这题脑子里立刻跳出链表..后来继续看如家的分析说,链表法时间复杂度为O(n*k),肯定会TLE,自己才意识到果然自个儿又头脑简单了 T^T. 看如家的分析没怎么看懂,后来发现这篇自己理解起来更 ...

  7. F - System Overload(约瑟夫环变形)

    Description Recently you must have experienced that when too many people use the BBS simultaneously, ...

  8. hdu 1443 Joseph (约瑟夫环)

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

  9. (顺序表的应用5.4.2)POJ 1591 M*A*S*H(约瑟夫环问题的变形——变换步长值)

    /* * POJ_1591_2.cpp * * Created on: 2013年10月31日 * Author: Administrator */ #include <iostream> ...

随机推荐

  1. NodeJS淘宝 CNPM 镜像

    原文地址:http://npm.taobao.org/ 设置NPM镜像(前提已安装NodeJS): npm config set registry https://registry.npm.taoba ...

  2. NIO之DatagramChannel

    Java NIO中的DatagramChannel是一个能收发UDP包的通道.操作步骤: 1)打开 DatagramChannel 2)接收/发送数据 同样它也支持NIO的非阻塞模式操作,例如: pu ...

  3. MII、GMII、RMII、SGMII、XGMII、XAUI、Interlaken

    MII即媒体独立接口,也叫介质无关接口.它是IEEE-802.3定义的以太网行业标准.它包括一个数据接口,以及一个MAC和PHY之间的管理接口(图1).数据接口包括分别用于发送器和接收器的两条独立信道 ...

  4. 283. Move Zeroes【easy】

    283. Move Zeroes[easy] Given an array nums, write a function to move all 0's to the end of it while ...

  5. 用lua nginx module搭建一个二维码

    用lua nginx module搭建一个二维码(qr code)生成器 作者 vinoca 發布於 2014年10月31日 如果有VPS,或者开源的路由器,安装一个nginx,添加lua-nginx ...

  6. sendmail发件人邮箱设定命令

    sendmail发件人邮箱设定命令   以前就碰到过设置发件人后缀的方式,这次迁移服务器居然忘记,从头开始记录下 1:第一种方法,修改/etc/hosts,据说sendmail使用hosts里面的本地 ...

  7. hdu1873 看病要排队 优先队列

    看病要排队 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  8. FMDB使用(转载)

    来自会员pengtao的分享:(原文:https://github.com/ccgus/fmdb) 由于FMDB是建立在SQLite的之上的,所以你至少也该把这篇文章从头到尾读一遍.与此同时,把SQL ...

  9. ftp 下载最近一小时的文件

    #!/bin/bash #cd /home/ftptmp/ ftp -v -n 192.168.0.100 2121 <<EOF user  test  2009 binary cd OU ...

  10. php 获取当前域名

    #测试网址: http://localhost/blog/testurl.php?id=5 //获取域名或主机地址 echo $_SERVER['HTTP_HOST']."<br> ...