题目描述:
The problem is named after Flavius Josephus, a Jewish historian who participated in and chronicled the Jewish revolt of 66-70C.E. against the Romans. Josephus, as a general, managed to hold the fortress of Jotapata for 47days, but after the fall of the city he took refuge with 40 diehards in a nearby cave. There the rebels voted to perish rather than surrender. Josephus proposed that each man in turn should dispatch his neighbor, the order to be determined by casting lots. Josephus contrived to draw the last lot, and as one of the two surviving men in the cave, he prevailed upon his intended victim to surrender to the Romans. Your task:computint the position of the survivor when there are initially n people.
输入:a Positive Integer n is initially people. n< = 50000
输出:the position of the survivor
样例输入:10
样例输出:4
约瑟夫问题
这里举的栗子是总人数为10,报数为4,从零开始报数。(题+代码和举的栗子不一样!!题目的报数限定为2)
∵新环编号 = (旧环编号 - 最大报数值) % 旧的总人数
∴旧环编号 = (新环编号 + 最大报数值) % 旧的总人数
#include<iostream>
using namespace std;
int main(){
const int m = 2;//最大报数值
int n;//总人数 也是要进行循环的轮数
int f = 0;//最后一个人报的数
cin >> n;
for(int i = 1;i <= n;i++){
f = (f + m) % i;
}
cout << f + 1 << endl;//注意从0开始报数和从1开始报数的区别
return 0;
}