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. HDU 1813 Escape from Tetris (IDA*)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1813 题意:给你一个n*n的迷宫,其中0代表有一个人在这个位置,1代表墙,现在要求一个路线,使所有的人通 ...

  2. Qt快速入门系列教程目录

    Qt快速入门系列教程目录

  3. [springmvc+mybatis][关于这两个框架的学习,我想说]

    关于学习笔记 在对java web有了一定的了解后,这两个框架没怎么写学习笔记了…毕竟项目驱动型…… 关于学习资料 强烈推荐传智播客的燕青讲解的 让我对这种培训班教育的资料刮目相看(不过还是千万别去这 ...

  4. MySql命令——表相关

    auto_increment //自动增长 primary key(id) //指定主键 select last_insert_id();//获得添加列的主键值 create table produc ...

  5. border-radius讲解2

    一:border-radius只有一个取值时,四个角具有相同的圆角设置,其效果是一致的: .demo { border-radius: 10px; } 其等价于: .demo{ border-top- ...

  6. Biztalk2010安装及配置问题集

    在安装Biztalk2010时,碰到很多问题,有的是粗心有的也是比较bt的,如: 1)在win7 64下引入x86 的cab,有点粗心,幸亏给我报错版本不兼容(呵呵): 2)安装的时候 不知道为什么计 ...

  7. UIAlertController(警告栏) 自学之初体验

    UIAlertController有两种样式  preferredStyle: UIAlertControllerStyleAlert (位于屏幕的中部) UIAlertControllerStyle ...

  8. shell中的expr命令

    expr 可以进行的操作如下: 逻辑操作 arg1 | arg2 逻辑或操作,真则返回arg1,否则返回arg2(以null或者0来判断参数的真假,有短路功能) arg1 & arg2 逻辑与 ...

  9. 【Solr专题之九】SolrJ教程

    一.SolrJ基础 1.相关资料 API:http://lucene.apache.org/solr/4_9_0/solr-solrj/ apache_solr_ref_guide_4.9.pdf:C ...

  10. event和window.event

    type:事件的类型,如onlick中的click:srcElement/target:事件源,就是发生事件的元素:button:声明被按下的鼠标键,整数,1代表左键,2代表右键,4代表中键,如果按下 ...