模拟约瑟夫环

 Roman Roulette 

The historian Flavius Josephus relates how, in the Romano-Jewish conflict  of 67 A.D., the Romans took the town of Jotapata which he was commanding.   Escaping, Jospehus found himself trapped in a cave with 40 companions.  The  Romans discovered his whereabouts and invited him to surrender, but his  companions refused to allow him to do so.  He therefore suggested that they  kill each other, one by one, the order to be decided by lot.  Tradition has it  that the means for effecting the lot was to stand in a circle, and, beginning  at some point, count round, every third person being killed in turn.  The  sole survivor of this process was Josephus, who then surrendered to the  Romans.  Which begs the question: had Josephus previously practised quietly  with 41 stones in a dark corner, or had he calculated mathematically that he  should adopt the 31st position in order to survive?

Having read an account of this gruesome event you become obsessed with  the fear that you will find yourself in a similar situation at some time in  the future.  In order to prepare yourself for such an eventuality you decide  to write a program to run on your hand-held PC which will determine the  position that the counting process should start in order to ensure that you  will be the sole survivor.

In particular, your program should be able to handle the following variation  of the processes described by Josephus.  n > 0 people are initially  arranged in a circle, facing inwards, and numbered from 1 to n.  The  numbering from 1 to n proceeds consecutively in  a clockwise direction.   Your allocated number is 1.  Starting with person number i, counting  starts in a clockwise direction, until we get to person number k (k > 0),  who is promptly killed.  We then proceed to count a further k people in a  clockwise direction, starting with the person immediately to the left of the  victim.  The person number k so selected has the job of burying the  victim, and then returning to the position in the circle that the victim had  previously occupied.  Counting then proceeds from the person to his  immediate left, with the kth person being killed, and so on, until only one  person remains.

For example, when n = 5, and k = 2, and i = 1, the order of execution is  2, 5, 3, and 1.  The survivor is 4.

Input and Output

Your program must read input lines containing values for n and k (in  that order), and for each input line output the number of the person with  which the counting should begin in order to ensure that you are the sole  survivor.  For example, in the above case the safe starting position is 3.   Input will be terminated by a line containing values of 0 for n and k.

Your program may assume a maximum of 100 people taking part in this  event.

Sample Input

1 1
1 5
0 0

Sample Output

1
1

UVa 130 - Roman Roulette的更多相关文章

  1. Roman Roulette(约瑟夫环模拟)

    Roman Roulette Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  2. UVA - 185 Roman Numerals

    题目链接: https://vjudge.net/problem/UVA-185 思路: 剪枝.回溯 注意回溯的时候,是从当前点的下一个开始,而不是从已经遍历的个数点开始!!不然回溯有问题! 思路参考 ...

  3. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

  4. python随机数学习笔记

    #coding:utf-8 import random # random.randint(1,10)产生1,10的随机整数 for i in range(1,5): ranint = random.r ...

  5. ascii码所有字符对照表(包含汉字和外国文字)

    http://www.0xaa55.com/thread-398-1-1.html看到了0xaa55的这个帖子,想起了2年前我在51cto发的一个帖子http://down.51cto.com/dat ...

  6. uva 759 - The Return of the Roman Empire

    #include <cstdio> #include <string> #include <map> using namespace std; ; , , , , ...

  7. UVA 590 二十一 Always on the run

     Always on the run Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  8. UVa 10048: Audiophobia

    这道题要求我们求出图中的给定的两个节点(一个起点一个终点,但这是无向图)之间所有“路径中最大权值”的最小值,这无疑是动态规划. 我开始时想到根据起点和终点用动态规划直接求结果,但最终由于题中S过大,会 ...

  9. [uva] 10067 - Playing with Wheels

    10067 - Playing with Wheels 题目页:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Ite ...

随机推荐

  1. [4G]Linux平台上实现4G通信

    转自:http://blog.sina.com.cn/s/blog_7880d3350102wb92.html 在ARM平台上实现4G模块的PPP拨号上网,参考网上的资料和自己的理解,从一无所知到开发 ...

  2. java中的方法——重载yu重写(转)

    重载(Overloading) (1) 方法重载是让类以统一的方式处理不同类型数据的一种手段.多个同名函数同时存在,具有不同的参数个数/类型. 重载Overloading是一个类中多态性的一种表现. ...

  3. win7、vs2013编译webkit总结

    在windows7.vs2013环境下编译webkit,本人测试成功 1.解压cygwin到C盘下(建议放在C盘更目录下,否则编译期间会有部分文件找不到) 启动命令:C:\cygwin\bin\min ...

  4. linux访问ftp服务器命令

    在linux访问时输入 用户名 anonymous 密码直接回车可以匿名登录 如果传输非文本,先输入bianry,然后再get就可以了 比如 ftp> get 5-4.tif 227 Enter ...

  5. 修改ES分片规则

    转自:http://my.oschina.net/crxy/blog/422287?p=1 Es查询的时候默认是随机从一些分片中查询数据,可以通过配置让es从某些分片中查询数据 1:_local 指查 ...

  6. 控件EditText

    (一)监听EditText输入内容变化 EditText.addTextChangedListener(textWatcher); //EditText change listener    //此方 ...

  7. RL Problems

    1.Delayed, sparse reward(feedback), Long-term planning Hierarchical Deep Reinforcement Learning, Sub ...

  8. Spring事务:调用同一个类中的方法

    问题: 如果同一个类中有方法:methodA(); methodB().methodA()没有开启事务,methodB()开启了事务 且methodA()会调用methodB(). 那么,method ...

  9. e638. 向剪切板获取和粘贴图像

    // If an image is on the system clipboard, this method returns it; // otherwise it returns null. pub ...

  10. php微信开发 -- 两种运营模式及服务器配置

    微信的两种运营模式 编辑模式:使用微信公众平台提供的功能 开发者模式:通过腾讯的api接口调用相应程序进行二次开发 编辑模式 应用场景: l 不具备开发能力的运营者 l 主要是进行品牌宣传.新闻媒体. ...