Josephus问题.cpp
#include<iostream>
using namespace std;
const int number=3; //假如报3的人退出圈子
const int n=5; // 假如人数为5
int main()
{
int a[n];
int i;
int k=1,j;
for(i=0;i<n;i++)
{
a[i]=i+1;
cout<<a[i]<<" ";
}
cout<<endl;
int m=-1; // 作为数组下标
while(true)
{
for(j=0;j<number;)
{
m=(m+1)%n;
if(a[m]!=0)
{
j++;
}
}
if(n==k)
{
break;
}
cout<<a[m]<<" ";
a[m]=0;
k++;
}
for(i=0;i<n;i++)
{
if(a[i]!=0)
{
cout<<"最后留下的是 "<<a[i];
}
}
return 0;
}