AcWing 3559. 围圈报数
考点:约瑟夫环问题,环形链表,队列
#include <bits/stdc++.h> using namespace std; const int N = 55; int ne[N];//链表指针数组 int main() { ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); int t; cin >> t; while (t--) { int n; cin >> n; for (int i = 1; i < n; i++) ne[i] = i + 1;//环形链表初始化,1->2, 2->3,... ne[n] = 1;//最后一个点指向1 int p = n;//p初始时指向1的前一个数。 while(n--) { p = ne[ne[p]];//走两步(数到三时删除) cout << ne[p] << ' '; ne[p] = ne[ne[p]];//改变环形结构,相当于把报数到3的人踢出 } cout << '\n';//注意此处不能使用puts(""); } return 0; }
注:puts()函数包含在头文件
#include<cstdio>
中,关闭cin, cout同步流之后该头文件#include<cstdio>
中的函数一律无法使用
本文作者:胖柚の工作室
本文链接:https://www.cnblogs.com/pangyou3s/p/17795776.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步