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

發佈了30 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章