约瑟夫问题

#include <iostream>
using namespace std;
bool a[101];
int main()
{
int n,m;
cin>>n>>m;
cout<<endl;
for(int i=1;i<=n;++i)
   a[i]=false;//全部标记住 
  /*输入:8 5
         输出:5 2 8 7 1 4 6 3
        */ 
int f=0,t=0,s=0;
do
{
++t;
if(t==n+1) t=1;//保证环 
if(a[t]==false) ++s;//如果没访问过就累加上。 
if(s==m)//5
{
s=0;//重新计数 
cout<<t<<" ";
a[t]=true;//说明这个人已经用过了。
++f; 
}
}while(f!=n);//直到所有人都跳出。 
return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41882322/article/details/80094129