# POJ 1012 Joseph

### 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.

### translation

/*
author:wangqi
url：http://poj.org/problem?id=1012

删除下标为(4+5-1)%5=3的人，4~4的人变为3~3；
删除下标为(3+5-1)%4=3的人，此时坏人全部删除。
*/
#include<iostream>
using namespace std;
int main(){
int m,k,i,result[15] = {0}; //result数组用于打表，猜测有很多重复的
while(cin>>k && k!= 0){
if(result[k] != 0){
cout<<result[k]<<endl;
continue;
}
for(m = k+1;;m++){
int num = 0;
for(i = 0 ; i < k; i++){
num = (num + m -1)%(2*k-i); //下标
if(num < k){ //num<k表示在全部坏人未删除完之前，删除了好人
break;
}
}
if(i == k){
cout<<m<<endl;
break;
}
}
result[k] = m;
}
}


07-31 1万

10-22 1360

07-13 529

#### poj 1012解题报告

2010年04月02日 3KB 下载

09-07 639

11-18 941

05-25 3424

08-19 618

04-10 177

09-29 462