且歌且行,眉目轻盈。何妨吟啸且徐行。|

胖柚の工作室

园龄:2年1个月粉丝:2关注:15

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 中国大陆许可协议进行许可。

posted @   胖柚の工作室  阅读(10)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起