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. ASP.NET中时间的绑定和格式化

    1.Eval和Bind的区别  绑定表达式  <%# Eval("字段名") %>  <%# Bind("字段名") %> 区别 1.e ...

  2. 7. 稀疏表示之OMP,SOMP算法及openCV实现

    一.前言 稀疏表示是自上世纪90年代开始,从人眼的视觉感受野获得启示,逐渐被人们所研究.现在已经发展为一种重要的信息表示方法.所谓稀疏表示是指,一个信号在过完备字典中,可以由少数个原子线性表达, 其数 ...

  3. 我跟着siki学Unity3D游戏开发——PongGame

    一.屏幕坐标转换为世界坐标. 1.游戏逻辑,根据界面布局,将墙体控制到对应的位置: vector3 position=Camer.main.ScreenToWorldPoint(new vetor2( ...

  4. 进阶笔记(1)——JavaScript 语言精碎

    调用:(调用一个函数将暂停当前函数的执行,传递控制权和参数给新函数) 每个函数接受连个附加参数:this (取决于调用的模式).argument. js的四种调用模式及this指向: 1.方法调用:( ...

  5. Python中的Copy和Deepcopy

    一,Python的对象: Python存在大量的对象,我们一般提到的对象都是C中的结构体在堆中申请的一块内存(以CPython为例),每一个对象都有ID,可以通过ID(Object)获得.对象的范围包 ...

  6. jquery判断表单提交是否为空

    <input type="text" value="" name="toPage"> 判断该表单是否为空 var p = $(& ...

  7. Mysql MEMORY 引擎

    CREATE TABLE `m` ( `) unsigned NOT NULL AUTO_INCREMENT, `name` ) NOT NULL, `ctime` ) NOT NULL, `ltim ...

  8. R语言数据合并使用merge数据追加使用rbind和cbind

    R语言中的横向数据合并merge及纵向数据合并rbind的使用 我们经常会遇到两个数据框拥有相同的时间或观测值,但这些列却不尽相同.处理的办法就是使用merge(x, y ,by.x = ,by.y ...

  9. 15个顶级Java多线程面试题及回答

    Java 线程面试问题 在任何Java面试当中多线程和并发方面的问题都是必不可少的一部分.如果你想获得任何股票投资银行的前台资讯职位,那么你应该准备很多关于多线程 的问题.在投资银行业务中多线程和并发 ...

  10. 浅谈HashMap的实现原理

    1.    HashMap概述: HashMap是基于哈希表的Map接口的非同步实现.此实现提供所有可选的映射操作,并允许使用null值和null键.此类不保证映射的顺序,特别是它不保证该顺序恒久不变 ...