The Josephus Problem

题目描述:
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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值