# POJ 1012：Joseph

586人阅读 评论(0)

Joseph
 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 50068 Accepted: 19020

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

6个人：1 2 3 4 5 6

m=5

kill_num=(kill_num+m-1)%rest

(找m)代码：

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;

int people[50];
vector <int> kill;

int main()
{
int n,k=0;
while(cin>>n)
{
int result=n+1,rest=2*n,kill_num=1;
int n2=2*n;

memset(people,0,sizeof(people));
kill.clear();
while(1)
{
if(kill.size()==n)
break;
if((result+kill_num-1)%rest==0)
{
kill_num=rest;
rest--;
kill.push_back(rest);
}
else if((result+kill_num-1)%rest<=n)
{
kill_num=1;
kill.clear();
rest=n2;
result++;
}
else
{
kill_num=(result+kill_num-1)%rest;
rest--;
kill.push_back(kill_num);
}
}
cout<<result<<endl;
}

return 0;
}

#include <iostream>
using namespace std;

int main()
{
int result[16];
int n;

result[1] = 2;
result[2] = 7;
result[3] = 5;
result[4] = 30;
result[5] = 169;
result[6] = 441;
result[7] = 1872;
result[8] = 7632;
result[9] = 1740;
result[10] = 93313;
result[11] = 459901;
result[12] = 1358657;
result[13] = 2504881;
result[14] = 13482720;

while(cin>>n && n)
{
cout<<result[n]<<endl;
}
return 0;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：169370次
• 积分：6040
• 等级：
• 排名：第4159名
• 原创：448篇
• 转载：6篇
• 译文：0篇
• 评论：82条
最新评论
个人说明
教育背景：
BUPT

编程语言：
C/C++ Python