一、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.

二、问题分析

        刚开始的时候没看懂英文,找了个翻译:约瑟夫问题是个有名的问题:N个人围成一圈,从第一个开始报数,第M个将被杀掉,最后剩下一个,其余人都将被杀掉。例如N=6,M=5,被杀掉的人的序号为5,4,6,2,3。最后剩下1号。假定在圈子里前K个为好人,后K个为坏人,你的任务是确定这样的最少M,使得所有的坏人在第一个好人之前被杀掉。这道题感觉不容易,看了很多提示才有点感觉。

       开始的时候总想着用什么数据结构实现这个环,什么链表,栈之类的。又总是琢磨从0开始还是从1开始,傻逼逼地在那比对公式的输出结果数字。后来发现完全没有必要,因为好人与坏人总是分在两个K内的。好人在小的那一部分,坏人在较大的那部分。所以只要下个要杀的人>k就可以继续循环。这里比较坑爹的是,测试数据里面有相同的K,如果不用记忆化存储的话,可能会超时。所以,这里好多人就干脆打表了。

       什么是打表呢?如果第一个测试用例让你求第100个,那么你可以将前100个数据在自己电脑里算好再存起来,这样如果题目再问到小于100的情况,就我们就可以直接输出了,相当于查表输出,时间耗时就自然少的多了。如果第二个测试用例让我们求第200个,那么我们就从第101个算起,算出的结果继续存起来,以备下一次的实用,如此类推,这个存起来的过程就叫做打表。(打表法貌似被不少人鄙视)

      不用打表其实也是可以过的,前辈们总结了三点,再次小弟引用一下:

            1.要kill的人的位置公式p=(p+m-1)%rest+1

            2.kill的位置<k就break,此时剩下的人rest等于k就成功

            3.m不要递增,如6个人,m取1,2,3第一次就杀了好人了,没意义。m是k+1的整数倍或者k+1的整数倍加1,这样会提高不少。
import java.util.Scanner;

public class Poj1012_Joseph {

	public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
int res[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//1~14,0位置不存放
int k=0;
while((k = cin.nextInt()) != 0){
int m=0;
if(res[k]==0){
while(true){
if(m/k==0)
m += k+1;
else
m ++;
int rest=2 * k;
int pos=0;
while(true){
pos=(pos+m-1)%rest+1; //位置公式
if(pos>k){
pos--;
rest--;
}else{
break;
}
}
if(rest==k){
res[k]=m;
break;
}
}
}
System.out.println(res[k]);
}
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Poj1012_Joseph的更多相关文章

随机推荐

  1. 数据处理 数据入数据库 与 Excel

    Python  数据处理   中间数据 Excel   团队交流分工   低的沟通成本    数据入数据库 如postgresql

  2. 【网络与系统安全】20179209 wireshark和nmap实验

    TCP三次握手包 在进行实验前,先梳理一遍TCP三次握手的过程,这个图是我本科学网络时画过不少于十遍的图,我觉得非常有纪念意义. 稍作解释,第一次握手,客户端发起请求连接,发送一个标志位为SYN的ip ...

  3. shell脚本调试运行

    1.在命令行提供参数:$sh -x script.sh   但是有的shell脚本只能用 ./xxx.sh的方式运行,不能用sh命令解析执行.则此方法会报错.2.脚本开头提供参数:#!/bin/sh ...

  4. Django开发模式会加载两次settings文件导致RotatingFileHandlerError

    当使用RotatingFileHandler作为django的日志处理器的时候,会报: Traceback (most recent call last): File "C:\Python2 ...

  5. python-安装 pip

    https://pip.pypa.io/en/stable/installing/ wget https://bootstrap.pypa.io/get-pip.py python get-pip.p ...

  6. 如何下载symfony

    php -r "readfile('https://symfony.com/installer');" > symfony 可能无法下载,:那么你检查一下你的php.ini找 ...

  7. 路由helper

    root_url http://192.168.1.110:3000/users/sign_up?inviter=14658733081530 root_path /users/sign_up?inv ...

  8. php 实现简单抽奖

    首先有一组数据,里面有中奖的物品和概率 $base_data = [ ['name'=>'特等奖','num'=>1], ['name'=>'一等奖','num'=>5], [ ...

  9. iOS中成员变量和属性区别

    历史由来: 接触iOS的人都知道,@property声明的属性默认会生成一个_类型的成员变量,同时也会生成setter/getter方法. 但这只是在iOS5之后,苹果推出的一个新机制.看老代码时,经 ...

  10. CSS3垂直图标菜单

    在线演示 本地下载