题目链接:

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. Python绘制分段函数

    1.绘制分段函数:y=4sin(4πt)-sgn(t-0.3)-sgn(0.72-t) import numpy as npimport matplotlib.pyplot as plt#绘制分段函数 ...

  2. sql笔记 获取指定数据库下的所有表

    SELECT Name FROM 数据库名称..SysObjects Where XType='U' ORDER BY Name XType='U' 标识查询用户创建的表 S为系统创建的表

  3. css3中-moz、-ms、-webkit,-o分别代表的意思

    这种方式在业界上统称:识别码.前缀 //-ms代表[ie]内核识别码 //-moz代表火狐[firefox]内核识别码 //-webkit代表谷歌[chrome]/苹果[safari]内核识别码 // ...

  4. form表单右边弹窗提示不能为空

    if (key == null || key == "") { layer.tips('流程标识key不能为空', $('#search_input'), { tips: [3, ...

  5. linq基础,正则表达式,.net相关

    声明扩展方法的步骤:类必须是static,方法是static,第一个参数是被扩展的对象,前面标注this.使用扩展方法的时候必须保证扩展方法类已经在当前代码中using.  LINQ:将int数组中大 ...

  6. 使用httpClient发送get\post请求

    maven依赖 <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId&g ...

  7. 【ASP.NET】——AdRotator控件

    AdRotator控件即广告控件. 广告,是站点不可缺少的一部分.也是站点获取收益的最主要途径,但最初制作广告非常麻烦.asp.net就将该方法封装成了一个控件.为我们节省了非常多时间.这也是ASP. ...

  8. View拖拽 自定义绑定view拖拽的工具类

    由于工作需求,需要用到这种处理方法所以我就写了这个 废话不多说先看效果图 接下来就看代码吧 DragDropManager import android.app.Activity; import an ...

  9. svn版本库目录结构

      该文是svn源代码分析系列文章服务端架构中的一篇,主要描述svn服务端版本库数据存储目录结构,并且对这些文件以及目录的作用进行简单分析.使用“svnmadin create”命令创建初始化版本库后 ...

  10. Tensorflow如何选择GPU

    CUDA_VISIBLE_DEVICES=1 python run.py or import os os.environ["CUDA_VISIBLE_DEVICES"]=" ...