Joseph problem is also called Joseph permutation. The game in this problem can be described like this. Several people stand in a circle to wait for being "killed". Now start counting clockwisely from a specific person in the circle, and "kill" a person when jump over a fixed number of alive people, and then repeat this operation until only one person alive who will also be released later. The problem is to select a position that will allow you alive ultimately. Now there are 10 people, standing in a circle numbered from 1 to 10. Count from #1, then #2, ..., and kill a person when jump over 5 alive people, in this case, #6 will be killed in the first round. Who will be alived in the end?
#include<iostream> using namespace std; int main() { int A[10]; for (int i = 0; i < 10; i ++) //all people are alive at the beginning { A[i] = true; } int pos = 0; for (int i = 1; i < 10; i ++)//进行9次淘汰 { int cnt = 0; //定义每次计数活下来的人的数目,初始化为0 while (true) { if (A[pos]) //当是活下来的人时,计数+1 cnt ++; if (cnt == 6) break;//当计数为6时终止循环,我们要把第六个人淘汰掉 pos = (pos + 1) % 10; //每一次位置向右边移动一位 } A[pos] = false;// 把第六个人淘汰掉 } for (int i = 0; i < 10; i++) if (A[i]) cout << i+1 << endl; return 0; }