/*
Time:2019.10.31
Author: Goven
type:约瑟夫环问题
ref:https://www.cnblogs.com/yu-chao/archive/2011/05/29/2062276.html
*/
#include<iostream>
using namespace std;
bool test(int k, int m) {
int n = 2 * k;
int j = 0;
for (int i = 0; i < k; i++) {//进行k次报数
j = (j + m - 1) % (n - i);
if (j < k) return false;
}
return true;
}
int main()
{
int Joseph[14] = {0};
for (int i = 1; i < 14; i++) {//表示人数的一半
int j = i + 1;//表示报数
while(1) {
if (test(i, j)) {
Joseph[i] = j;
break;
}
if (test(i, j + 1)) {
Joseph[i] = j + 1;
break;
}
j = i + j + 1;//Att:为何能够替代成j++? 见ref
}
}
int k;
while (cin >> k && k) {
cout << Joseph[k] << endl;
}
return 0;
}
网友评论