可否解释一下nth_victim? 我知道是其中涉及数学...请大家用数学解释一下...谢谢了...
/*---------------------------------------------------------------------------
File name: Josephus_nth_victim.cpp
Author: HJin (email: fish_sea_bird [at] yahoo [dot] com )
Created on: 8/11/2007 20:23:48
Environment: Windows XP Professional SP2 English +
Visual Studio 2005 v8.0.50727.762
Modification history:
===========================================================================
Sample output
12 4 2
5 9 1 6 11 4 12 8 7 10 3 2
41 3 1
3 6 9 12 15 18 21 24 27 30 33 36 39 1 5 10 14 19 23 28 32 37 41 7 13 20 26 34 40
8 17 29 38 11 25 2 22 4 35 16 31
*/
#include <iostream>
using namespace std;
/** nth_victim of the Josephus circle.
* @param n --- number of people
* @param m --- step number
* @param s --- start
* @param k --- kth victim
* @return returns the original index of the kth victim.
*/
int nth_victim(int n, int m, int s, int k)
{
int p=k*m;
while(p>n)
p=(m*(p-n)-1)/(m-1);
p=(p+s-1)%n;
if(p==0)
p=n;
return p;
}
int main()
{
int n, m, s, k;
while(cin>>n>>m>>s)
{
for(k=1; k<=n; ++k)
cout<<nth_victim(n, m, s, k)<< " ";
cout<<endl<<endl;
}
return 0;
}