# POJ 1012：Joseph

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;
}

07-31 1万

10-22 1364

07-13 530

#### poj 1012解题报告

2010年04月02日 3KB 下载

09-07 640

11-18 942

05-25 3424

09-29 462

09-16 309

08-19 620