您的位置:首页 > 编程语言 > PHP开发

PKU1012 约瑟夫问题

2010-03-17 22:29 399 查看
Joseph


Time Limit:

1000MS

 

Memory Limit:

10000K

Total Submissions:

27412

 

Accepted:

10260

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

 

经过这题才知道原来有打表这东西
……

 

//PKU1012
#include<iostream>
using namespace std;
int main()
{
int i,j,k,l,p,n,m,a[14];
for (k=1;k<14;++k)
{
m=1;
p=1;
l=0;
while(p)
{
l++;
if (l&1) m+=k;else m++;
n=k<<1;
i=1;
while (1)
{
if (i+m-1>n) i=(i+m-1)%n;else i=i+m-1;
if (i==0) i=n;
if (i<=k) break;
n--;
}
if (n==k) p=0;
a[k]=m;
}
}
while (scanf("%d",&k)&&k) printf("%d/n",a[k]);
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  input output file