这只是输出最后的胜利者。。。
n 代表总人数, m 代表报的数。
public class Josephus
{
public static int Josephus(int n, int m)
{
if (n == 1)
{
return 0;
}
else
{
return (Josephus(n - 1, m) + m) % n;
}
}
public static void main(String[] args)
{
int[] a = {1, 2, 3, 4, 5, 6, 7};
int i = Josephus(a.length, 3);
System.out.println(a[i]);
}
}
假设已经知道n- 1个人最后的优胜者为Josephus(n-1),当增加1个人时,此时要多报一轮数m, 这时优胜者也要往后移动m个位置,移到下一个安全位置,此时要对n 取余了。。。