Joseph
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 44650   Accepted: 16837

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
题目大意:约瑟夫问题,总共有2 * k个人报数,前面k个是好人,后面k个是坏人,报道m的人要死去,问当m为何值时,坏人全部死去之前不会有好人死去。
解题方法:从1开始枚举m的值,遇到不满足的则将m加一再次枚举,知道满足为止。
#include <stdio.h>

int main()
{
int people[] = {}, k, Joseph[] = {};//Joseph用于打表,不然超时
while(scanf("%d", &k) != EOF && k != )
{
if (Joseph[k] != )
{
printf("%d\n", Joseph[k]);
continue;
}
int n = * k;
int m = ;
people[] = ;//people[0] = 0表示编号从0开始
for (int i = ; i <= k; i++)
{
//每次枚举完毕将剩下的人按从0到n - i编号,只要好人没有杀掉,则前面k - 1个编号是不变的
people[i] = (people[i - ] + m - ) % (n - i + );
if (people[i] < k)//第k个人的编号为k - 1,所以这里是<而不是<=
{
i = ;
m++;
}
}
Joseph[k] = m;
printf("%d\n", m);
}
return ;
}

POJ 1012 Joseph的更多相关文章

  1. POJ 1012 Joseph 约瑟夫问题

    http://poj.org/problem?id=1012 早上去图书馆复习苦逼的复习....万恶的数逻.T T我还要自我安慰的说复习完了奖励回来刷水题~ 10点多的时候外面校运会大吼撑杆跳的那个. ...

  2. POJ 1012 Joseph 推导,暴力,约瑟夫环,打表 难度:2

    http://poj.org/problem?id=1012 答案以954ms飘过,不过这道题可以轻松用打表过 思路:如果我们把每个人位于数组中的原始编号记为绝对编号,每次循环过后相对于绝对编号为0的 ...

  3. poj 1012 Joseph (约瑟夫问题)

    Joseph Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 47657   Accepted: 17949 Descript ...

  4. POJ 1012 Joseph(打表题)

    题意:约瑟夫环的变形.要求寻找到一个杀人循环节m使后半节的坏人先被所有杀光. 直接链表模拟出结果,再打表即可: 代码:(凝视的是打表码) #include<iostream> #inclu ...

  5. poj 1012 &amp; hdu 1443 Joseph(约瑟夫环变形)

    题目链接: POJ  1012: id=1012">http://poj.org/problem?id=1012 HDU 1443: pid=1443">http:// ...

  6. poj 1012——Toseph

    提交地址:http://poj.org/problem?id=1012                                                                 ...

  7. POJ 1012:Joseph

    Joseph Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 50068   Accepted: 19020 Descript ...

  8. Joseph POJ - 1012 约瑟夫环递推

    题意:约瑟夫环  初始前k个人后k个人  问m等于多少的时候 后k个先出去 题解:因为前k个位置是不动的,所以只要考虑每次递推后的位置在不在前面k个就行 有递推式 ans[i]=(ans[i-1]+m ...

  9. POJ 1012

    参考自:https://www.cnblogs.com/ECJTUACM-873284962/p/6480880.html Joseph Time Limit: 1000MS   Memory Lim ...

随机推荐

  1. [游戏模版16] Win32 飞机射击 敌人追踪

    >_<:AI introduction. >_<:According the plane position (nowX,nowY) relative to birds' pos ...

  2. SSRS 迁移

    一.数据库备份 备份源数据库:ReportServer和ReportServerTempDB (注意是全备份) 二.数据库还原 还原之前先停掉SSRS 还原至目标数据库:ReportServer和Re ...

  3. Unity3d碰撞检测中碰撞器与触发器的区别

    要产生碰撞必须为游戏对象添加刚体(Rigidbody)和碰撞器,刚体可以让物体在物理影响下运动.碰撞体是物理组件的一类,它要与刚体一起添加到游戏对象上才能触发碰撞.如果两个刚体相互撞在一起,除非两个对 ...

  4. 如何试用Office 365 及 SharePoint Online(美版)

    Office 365已经在国外运营一段时间了,本文主要帮助大家注册一个试用账户.废话少说按步骤来: 进入注册页面,链接地址 . 1. 对于不同的企业,提供了不同的套餐,这里我们试用这个中等企业的套餐, ...

  5. Authentication in .NET Web Api

    在介绍WebApi这个小demo前,先来简单说一下OAuth这个东西. OAuth开放授权,用户可以对自己的资源进行第三方授权,那么第三方就可以不用你的账号密码就可以访问你授权的资源了.比如一些论坛直 ...

  6. Artificial Intelligence Language

    Artificial Intelligence Language Objective We know, a true AI program should have ability to underst ...

  7. 使用iMovie和Keynote制作App Preview

    App Preview是什么 App Preview就是一段15-30秒的短视频,用来展示你的应用的特性.用户界面.交互方式等内容.在App Store你的应用的详细信息页面里,放在原来的截图之前.体 ...

  8. C语言内存分布之数据段

    不管我们以后是自己写代码还是读别人的代码,都应该想想这个变量默认存储的位置.在我们以后的嵌入式开发中,技巧性的代码越来越多的时候,我们可能把某一些代码放在一段.我们可以通过修改变量或者代码默认放置的段 ...

  9. HBM内存介绍

    原帖地址:http://www.anandtech.com/show/9969/jedec-publishes-hbm2-specification The high-bandwidth memory ...

  10. PS脚本获取网络适配器状态

    1. Get-WmiObject -Class Win32_NetworkAdapterConfiguration ` -filter "IPEnabled = $true" 2. ...