题目链接:

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. Mac+docker+flask

    1.先要保证mac电脑安装docker 安装的话可以通过命令brew install docker 安装成功后启动,docker 然后在终端执行docker version 或者docker info ...

  2. rabbitmqctl 报错

    RabbitMQ 安装成功后,cmd dos命令进入RabbitMQ的安装路径,如 E:\Program Files\RabbitMQ Server\rabbitmq_server-3.6.10\sb ...

  3. nginx sever_name正则

    nginx server_name 规则: 1.确切的server_name匹配 例如: server { listen ; server_name www.luwen.cc luwen.cc; .. ...

  4. linux杂谈(十一):LDAPserver的搭建

    1.LDAP简单介绍      今天我们来介绍LDAPserver的搭建和client的訪问,可是基本的问题在前者.首先我们要知道什么是LDAP.      在日常交谈中.你可能会听到有些人这么说:& ...

  5. 83. Remove Duplicates from Sorted List【easy】

    83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...

  6. java - day10 - uptest

    package day03; public class UpTest { public static void main(){ Aoo o = new Aoo(); o.a = 3; o.b = 3; ...

  7. 使用pycharm手动搭建python语言django开发环境(五) 使用日志模块打日志

    1.在项目的settings.py中增加日志相关声明 #增加日志设置 LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'fil ...

  8. python3输出指定log信息

    问题背景: win10 python xxx.py > c:test.txt 上面这句只能把信息输出到test.txt,但是控制台看不到信息 ########################## ...

  9. log4j日志写入数据库

    # log4j写入数据库 ### 前言-----------------------------log4j是写入日志到控制台和文件很常见,但是写入到数据库不多见.做性能测试写入到数据库,统计方便些. ...

  10. 虚拟化笔记04.OpenFiler.install

    4.OpenFiler install OPENFILER 介绍 1.OpenFiler 作用 OpenFiler 基于Linux 内核,主要作用是实现IP-SAN. 在VSPHERE中我们可以将VM ...