题目链接:

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. Cannot find module 'webpack'

    执行webpack命令报错 Error: Cannot find module 'webpack' at Function.Module._resolveFilename (module.js:325 ...

  2. mvc模型绑定问题

    public void AddDesk(x_s_desk x_s_desk) 绑定的到 public void AddDesk(x_s_desk desk) 绑定不到 目前不知道原因 且仅有部分模型需 ...

  3. margin: 0px auto; center 行类 块级

    <html> <head> <title> biaoti </title> </head> <body style="bor ...

  4. find命令下排除部分目录修改权限

    #!/bin/bash # 项目文件夹.文件权限修改 # 批量修改文件夹或者文件的权限时,需要先忽略掉可写文件夹('./bootstrap/cache'.'./public/attachments'. ...

  5. MongoDB学习——持续更新

    參考MongoDB权威指南,学习阶段.大家多多交流问题.持续更新本文 MongoDB的长处 MongoDB具有丰富的数据模型,是面向文档的数据库. easy扩展.能够在多台server之间切割数据. ...

  6. 详解TCP建立连接全过程

    TCP是因特网中的传输层协议,使用三次握手协议建立连接,下面是TCP建立连接的全过程. 上图画出了TCP建立连接的过程.假定主机A是TCP客户端,B是服务端.最初两端的TCP进程都处于CLOSED状态 ...

  7. vue 过渡的-css-类名

    会有6个css类名在leave/enter过渡中切换 1,v-enter:定义进入过渡的开始状态,在元素被插入时生效,在下一帧中移除 2,v-enter-active:定义过渡的状态.在元素整个过渡过 ...

  8. DataUml Design 介绍9 - DataUML 1.3版本功能(查询分析器功能等)

    DataUML 1.3 (下载)主要更新内容如下: 1.增加查询分析器功能: 2.增加打开历史文件记录功能: 3.修改查询对象功能: 4.增加显示对象长度功能: 5.增加配置显示表字段功能: 6.增加 ...

  9. Leetcode392. Is Subsequence

    Description Given a string s and a string t, check if s is subsequence of t. You may assume that the ...

  10. chrono

    时间段的表示 tmplate<class Rep,class Period=ratio<1>> class duration; duration类被用来表示时间段的计量器,Re ...