Joseph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1652    Accepted Submission(s): 1031

Problem 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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1444 1397 1452 1725 1393 
 

模拟打表 但是写的异常慢 虽然过了 但是太慢了



代码如下
#include<stdio.h>
#include<string.h>
int m[30],a[30];
int main()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
int k,ok=1,t,i,num,flag,j;
while(scanf("%d",&k)!=EOF&&k!=0)
{
if(a[k]) {printf("%d\n",a[k]);continue;}
for(i=k+1;;i++)
{
num=k;ok=1;flag=1;j=1;
memset(m,0,sizeof(m));
while(num!=0)
{
t=i%(num+k);
if(t==0) t=num+k;
for(;t!=0;j++)
{
if(j>2*k) j=j%(2*k);
if(m[j]==0) t--;
}
j--;
if(1<=j&&j<=k){ok=0;break;}
else { m[j]=1;num--;}
} if(ok) {a[k]=i; printf("%d\n",i);break;}
}
}
}

翻了翻 DISCUSS 貌似有个写的十分优美的

#include <cstdlib>
#include <iostream>
#include<string>
#include <cstdio>
#include<algorithm>
#include <sstream>
#include <math.h>
using namespace std;
int n, a, b;
int con[15];
int run(int a, int b)
{
int cnt = 0;
int sum = 2 * a;
int index = b % (2 * a);
if(index == 0) index = sum; while(index > a)
{
sum--;
index = (index + b - 1) % sum;
if(index == 0) index = sum;
cnt ++;
}
return cnt;
} int solve(int a)
{
for(int i=a+1; ; i++)
{
if(run(a, i) == a) return i;
} }
void init()
{
for(int i=1; i<=14; i++)
{
con[i] = solve(i);
}
} int main()
{
//freopen("D:\\in.txt","r",stdin);
init();
while(cin>>n)
{
if(n == 0) break;
else cout<<con[n]<<endl;
}
return 0;
}

这段代码的区别就是 将所有坏人都看做一样的人,5 6 7 8是没有区别的 当7死后 8可以被当做7 用while(index>a)保证只有坏人出局

【模拟】【HDU1443】 Joseph的更多相关文章

  1. 一道模拟题:改进的Joseph环

    题目:改进的Joseph环.一圈人报数,报数上限依次为3,7,11,19,循环进行,直到所有人出列完毕. 思路:双向循环链表模拟. 代码: #include <cstdio> #inclu ...

  2. Joseph(hdu1443)

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

  3. HDU1443 模拟(难)

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

  4. Lucky and Good Months by Gregorian Calendar - POJ3393模拟

    Lucky and Good Months by Gregorian Calendar Time Limit: 1000MS Memory Limit: 65536K Description Have ...

  5. Joseph(JAVA版)

    package Joseph;//约瑟夫环,m个人围成一圈.从第K个人开始报数,报道m数时,那个人出列,以此得到出列序列//例如1,2,3,4.从2开始报数,报到3剔除,顺序为4,3,1,2publi ...

  6. Hdu 1443 Joseph

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

  7. UVA 305 Joseph (约瑟夫环 打表)

     Joseph  The Joseph's problem is notoriously known. For those who are not familiar with the original ...

  8. 华为机试 之 joseph环

    一:首先科普一下约瑟夫问题的数学方法 (1)  不管是用list实现还是用vector实现都有一个共同点:要模拟整个游戏过程,不仅程序写起来比較烦,并且时间复杂度高达O(nm),当n,m很大(比如上百 ...

  9. 约瑟夫环(Joseph)的高级版(面向事件及“伪链表””)

    约瑟夫环问题: 在一间房间总共有n个人(下标0-n-1),只能有最后一个人活命. 按照如下规则去杀人: 所有人围成一圈 顺时针报数,每次报到q的人将被杀掉 被杀掉的人将从房间内被移走 然后从被杀掉的下 ...

随机推荐

  1. HTTP协议1之协议详解--转

    当今web程序的开发技术真是百家争鸣,ASP.NET, PHP, JSP,Perl, AJAX 等等. 无论Web技术在未来如何发展,理解Web程序之间通信的基本协议相当重要, 因为它让我们理解了We ...

  2. 不同浏览器对URL最大长度的限制(转)

    1.今天碰到一个bug,window.open后面的页面,接收参数不全,导致后台报错.实验了一下.发现是使用get方法请求服务器时,URL过长所致 微软官方的说明: http://support.mi ...

  3. iOS系统相册的有关操作

    iOS中,我们选择相册中的资源和调用摄像头可以使用 :UIImagePickerController类来完成,不使用UI我们可以通过:ALAssetsLibrary类来使用相册资源. 一. ALAss ...

  4. POJ 2455 Secret Milking Machine (二分 + 最大流)

    题目大意: 给出一张无向图,找出T条从1..N的路径,互不重复,求走过的所有边中的最大值最小是多少. 算法讨论: 首先最大值最小就提醒我们用二分,每次二分一个最大值,然后重新构图,把那些边权符合要求的 ...

  5. 【转】cocos2d-x windows开发环境配置

    声明:本教程在参考了以下博文,并经过自己的摸索后实际操作得出,本教程系本人原创,由于升级后的cocos2d-x有了一些变化,目前的博文还没有关于Cocos2d-x 2.2.1最新版搭建Android交 ...

  6. struts2的注解配置全面解析

    以前在用struts2的注解配置时总是要在web.xml中配置一个初始化参数(actionPackages),最近发现不灵了,仔细研究了下发现即使不用在web.xml中配置也能成功,但时灵时不灵的,很 ...

  7. gerrit升级到16.04之后连接不到服务器

    升级到ubuntu-16.04后,发现Git-review代码报错: Unable to negotiate with 10.140.110.77 port 29418: no matching ke ...

  8. NET Core开发-获取所有注入(DI)服务

    NET Core开发-获取所有注入(DI)服务 获取ASP.NET Core中所有注入(DI)服务,在ASP.NET Core中加入了Dependency Injection依赖注入. 我们在Cont ...

  9. ES6新特性-------解构、参数、模块和记号(续)

    六.解构 解构提供了一个方便的地从对象或数组中提取数据的方法,请看下面的例子: //ES6 let [x,y]=[1,2];//x=1,y=2 //ES5 var arr=[1,2]; var x=a ...

  10. 如何使用fiddler进行android手机测试

    一.什么是Fiddler Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的设备和互联网之间的http通讯,设置断点,查看所有的“进出”Fiddler的数据,并且可以胡乱修改. F ...