使用STL list,本地通过洛谷RE,望有dalao指点

P1996 约瑟夫问题

输入 `4 2` RE,原因:`L.erase(it)` 之后 `it` 可能变成野迭代器
by Disjoint_cat @ 2023-10-12 14:27:01


为什么要用list 不要用标记数组更简单吗
by lao_wang @ 2023-10-12 14:31:51


@[Donotplaygame](/user/549499) 感谢,已AC 附AC代码(※码风不良) ```cpp #include<bits/stdc++.h> using namespace std; int n,m,now=1; list<int> L; int main() { cin>>n>>m; for(int i=1;i<=n;++i) { L.push_back(i); } list<int>::iterator it=L.begin(),tmp; while(!L.empty()) { if(it==L.end()) { it=L.begin(); continue; } if(now==m) { tmp=it; it++; cout<<*tmp<<" "; if(it==L.end()) { L.erase(tmp); it=L.begin(); now=1; continue; } else { L.erase(tmp); now=1; continue; } } if(L.empty()) { break; } now++; it++; } return 0; } ```
by qianzhu233 @ 2023-10-16 14:00:55


@[lao_wang](/user/701408) 想学一下stl里的list
by qianzhu233 @ 2023-10-16 14:01:52


可以用单向动态链表
by xujinxuan01 @ 2023-10-28 14:56:31


|