最新消息:点击查看大S的省钱秘笈

POJ 1012 Joseph C++版

POJ题解 Slyar 122浏览 0评论

文章作者:姜南(Slyar) 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。

算了,博客不知道发什么,继续刷水题...

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

Slyar:约瑟夫问题都知道了,这个题就是给出一个k,总人数n等于2k,让你找到一个报数m,使得后k个人先出列...

反正k才到14,暴力枚举就行了,不过由于数据比较多,需要开一个数组保存一下才不会超时...

转载请注明:Slyar Home » POJ 1012 Joseph C++版

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (4)

  1. chx : 我也想知道啊……
    我也是 我想知道
    匿名6年前 (2011-04-05)回复
  2. 我也想知道啊……
    chx7年前 (2010-07-08)回复
  3. @Yuan 哪有m+=k+1?貌似只有i += (k + 1)...i++为什么不行?因为不是每次都报1啊...
    Slyar7年前 (2009-11-12)回复
  4. 请问一下这里为什么 m+=k+1呢? m++为什么不行?
    Yuan7年前 (2009-11-12)回复