约瑟夫问题(前面有题很想这个,但是当时不是用的队列,在写一次那个方法还是错了,因为并没有理解,但是队列就很容易懂)
题解:
#include"iostream"
#include<queue>
using namespace std;
int main(){
int n,goal,nownumber = 1;
queue<int>q;
cin>>n>>goal;
for(int i = 1;i <= n; i++)
q.push(i);
while( ! q.empty()){
if(goal == nownumber){
nownumber = 1;
cout<<q.front()<<" ";
q.pop();
}
else{
nownumber++;
q.push(q.front());
q.pop;
}
}
cout<<endl;
return 0;
}