The Josephus Problem

      I recently find a class of questions called the josephus problem,all those questions is similar with the following:
    long long ago,there was a king,he is a mathematician but crue and brutall ,and he always invited many people to his palace,and let them sat around a table,and assigned everyone a number,assume there were 20 people,the numner is form 1 to 20.then the king counted them from nunber 1,when the total reached to 3,he will kill the person,and continue this from the next person ,and the result from 1,and when there were only 1 person, he will get 1 billion coins.if you were one of those people,which number you will sit to alive and get the 1 billion coins?
      This type of problem is called a "Josephus problem", after a story about a historian of the first century, Flavius Josephus, who survived the Jewish-Roman war perhaps due to his mathematical talents. In his book The Jewish Wars Flavius tells that he was one out of 41 Jewish rebels trapped by the Romans. His companions preferred suicide to escape, so they decided to draw lots to see who would kill whom so that they could avoid both capture and the sin of suicide. The idea that they decided to form a cycle and to kill every third person and to proceed around the circle until no one was left is probably a myth. According to the problem Josephus wasn't excited by the idea of killing himself, so he calculated where he has to stand to survive the vicious circle. The general Josephus problem; involves how many people will stand around a circle and how many shall be passed over before the next one is killed.
      We can consider this question in this way: if  there were only one person,he is the winner(he will get the 1 billion coins or not dead),his number is 1 in the circle,and we assume there were 2 people,the winner's number is 2 in this circle.
      Suppose there have n people,we list them beginning with 0 as the following:
      0 1 2 3 ....n-2 n-1 we assume the step is m which means we will kick the m person off.then his number must be (m-1)%n,and then left n-1 people,we also assume his number is k,and then the list is 0 1 2 ...k-1 k+1 ...n-2 ;we can change its order to k+1 k+2..1 2 ...n-2,now we assume again,the winner's number is x,What is the number of the winner in preceding cirle? I think this is very easy,the number equals to (k+x+1)%n,we know k=(m-1)%n,so the number of winner also equals to (x+m)%n.
      From preceding deducing,we can get the winner's number of  n people circle from n-1 people circle.and the 1 people circle winner's  is number 0 according to the preceding deducing.So this is a recursion:
the number we count is 3,which is also the m of the preceding paragraph.
total   index  number
1        0        1
2        1        2
3        1        2
4        0        1
5        3        4
      The programm in C++ programming language is the following:

#include<iostream.h>
void main()
{
    int total,step,i,result=0;
   cin>>total>>step;
    for(i=2;i<=total;i++)
    {
           result = (result+step)%i;
     }
     cout<<"the winner is:"<<result+1<<endl;
}

Date:2008-09-26

Place:Neusoft F3

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值