The Josephus Problem

The Josephus Problem
1000(ms)
65535(kb)
834 / 2034
The problem is named after Flavius Josephus, a Jewish historian whoparticipated in and chronicled the Jewish revolt of 66-70C.E.against the Romans. Josephus, as a general, managed to hold thefortress of Jotapata for 47days, but after the fall of the city hetook refuge with 40 diehards in a nearby cave. There the rebelsvoted to perish rather than surrender. Josephus proposed that eachman in turn should dispatch his neighbor, the order to bedetermined by casting lots. Josephus contrived to draw the lastlot, and as one of the two surviving men in the cave, he prevailedupon his intended victim to surrender to the Romans. Yourtask:computint the position of the survivor when there areinitially n people.
输入
a Positive Integer n is initially  people. n< = 50000
输出
the position of the survivor
样例输入
6
样例输出
5
@浅夏沫若.code:
#include
using namespace std;
int main()
{
 int n = 0;
 int flag = 0;
 int counter = 0;
 int a[50001] = {0};
 cin >> n;
 while (1)
 {
  for (int i = 1; i <= n;i++)
  {
   if (flag ==1&&a[i]==0)
   {
    a[i]= 1;
    counter= counter + 1;
    flag= 0;
    if(counter == n - 1)
     break;
   }
   else if (flag== 0 && a[i] == 0)
   {
    flag= 1;
   }
  }
  if (counter == n - 1)
   break;
 }
 for(int i=1;i<=n;i++)
  if (a[i] == 0)
  {
   cout <<i << endl;
  }
 return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值