高中就有在出现的约瑟夫环问题,可以用vis数组加a数组加上两个循环来解决,都什么年代了还在用传统数组,这道题可以换一个思路,这些孩子都在一个队列里面,从队首开始报数,如果没出圈,那么放到队尾去等着。
然后就AC了
AC代码
#include <bits/stdc++.h>
using namespace std;
int n,m,cnt;
queue<int>q;
int main(){
cin>>n>>m;
for(int i=1;i<=n;++i)q.push(i);
while(!q.empty()){
cnt=1;
while(cnt++!=m)q.push(q.front()),q.pop();
cout<<q.front()<<" ";
q.pop();
}
return 0;
}
也可以用STL的list来解决,因为内置结构是双向循环链表,不过erase某个节点的时候还要考虑再三,因为迭代器不会跟着动,所以不如queue方便。