很多人都在问约瑟夫问题该怎么解,我把基本型发上来。
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"请问有多少人?"<<endl;
cin>>n;
bool all[n+1];//all[0]不用
int times=0;
for(auto &a:all)
a=true;
int c=0;
int q;
cout<<"淘汰几人?"<<endl;
cin>>q;
int p;
cout<<"数到几淘汰?"<<endl;
cin>>p;
for(int b=0;b<q;++b)
{
while(1)
{
if(c<n)
++c;
else
c=1;
if(all[c])
++times;
if(times%p==0)
{
all[c]=false;
times=0;
break;
}
}
}
for(int a=1;a<=(n+1);++a)
cout<<all[a]<<" ";
return 0;
}
本人代码仅供参考。
如有任何错误,请在评论区指出,谢谢!