- 用数组或者队列模拟循环队列
- 数组:用ne数组表示当前这个的下一个元素,ne[n] = 1,这样就是循环数组了,在删除一个元素的时候记得将ne数组进行更新
- 队列:每次将开头的元素如果还要放回继续放回,否则直接输出
#include <iostream>
#include <queue>
using namespace std;
int main() {
int _; cin >> _;
while (_ -- ) {
int n;
cin >> n;
queue<int> que;
int cnt = 1;
for (int i = 1; i <= n; ++ i) {
que.push(i);
}
int k = n;
while (k) {
if (cnt == 3) {
cnt = 0;
k -- ;
cout << que.front() << ' ';
que.pop();
} else {
int now = que.front();
que.pop();
que.push(now);
}
cnt ++ ;
}
cout << endl;
}
}
#include <iostream>
#include <cstring>
using namespace std;
const int N = 55;
int n;
int ne[N];
int main() {
int _; cin >> _;
while (_ -- ) {
cin >> n;
memset(ne, 0, sizeof ne);
for (int i = 1; i < n; ++ i) {
ne[i] = i + 1;
}
ne[n] = 1;
int k = n, start = 1;
while (k) {
k -- ;
cout << ne[ne[start]] << ' ';
ne[ne[start]] = ne[ne[ne[start]]];
start = ne[ne[start]];
}
cout << endl;
}
}