【链表、队列】约瑟夫环问题
作者:互联网
3253. 游戏
#include <iostream>
#include <queue>
using namespace std;
int n, k;
bool check(int x)
{
if (x % k == 0 || (x % 10) == k) return true;
return false;
}
int main()
{
cin >> n >> k;
queue<int> q;
for (int i = 1; i <= n; i ++ ) q.push(i);
int j = 1; // 从1开始报数
while (q.size() > 1)
{
int t = q.front();
q.pop();
if (!check(j)) q.push(t); // 如果这个数不用出局,那就再加到队尾
j ++ ;
}
cout << q.front() << endl;
return 0;
}
3559. 围圈报数
#include <iostream>
using namespace std;
const int N = 55;
int n;
int ne[N];
int main()
{
int T;
cin >> T;
while (T -- )
{
cin >> n;
for (int i = 1; i < n; i ++ ) ne[i] = i + 1;
ne[n] = 1;
int p = n;
for (int i = 0; i < n; i ++ )
{
p = ne[ne[p]]; // 走两步
cout << ne[p] << ' ';
ne[p] = ne[ne[p]]; // 删掉一个节点
}
cout << endl;
}
return 0;
}
标签:cout,cin,队列,++,ne,约瑟夫,链表,int,include 来源: https://www.cnblogs.com/Tshaxz/p/16658925.html