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. last与lastlog命令

    lastlog 列出所有用户最后登录的时间和登录终端的地址,如果此用户从来没有登录,则显示:**Never logged in**last 列出用户所有的登录时间和登录终端的地址

  2. 《JavaScript 闯关记》之 DOM(上)

    DOM(文档对象模型)是针对 HTML 和 XML 文档的一个 API.DOM 描绘了一个层次化的节点树,允许开发人员添加.移除和修改页面的某一部分. 节点层次 DOM 可以将任何 HTML 或 XM ...

  3. 阿里云 Debian Linux 布署记录

    摘要: 主要安装了web环境,java+tomcat+mysql+nginx(暂没没用) 数据盘挂载在/data下,项目,索引都放/data目录下 java,tomcat,mysql程序都在/root ...

  4. MVC 界面开发

    1.什么是设计模式 mvc只是其中一种,对某一类具体问题,总结出来的一套最优的解决方案 1.MVC: 1.Model(模型)     View(视图)    Controller(控制器) 的缩写 M ...

  5. UISwitch 监听响应

    UISwitch *swh = [[UISwitch alloc]initWithFrame:CGRectMake(100,100, 50, 30)];        swh.on = YES;    ...

  6. js post传值

    一种是ajax传值,另一种是post传值, ajax传值: $.ajax({ url: "AjaxTxt/Fild.ashx?Name=duibi&dates=" suzk ...

  7. select2使用详解

    官网: https://select2.github.io/examples.html 引用: <link href="~/Scripts/select2/select2.css&qu ...

  8. dede调用指定栏目的标签

    {dede:type typeid='1'} <a href="[field:typelink /]">[field:typename /]</a> {/d ...

  9. 在CentOS 7下试验Drupal 7

    按顺序安装好Apache.MariaDB和PHP,启动Apache和MariaDB,创建一个UTF-8字符集的数据库. > create database if not exists drupa ...

  10. 一级域名301重定向到www二级域名

    301重定向有利于百度的搜索 例如一个域名 www.test.com如果不做设置会产生4个网址, (1)test.com(2)www.test.com(3)test.com/default.html( ...