AcWing 3559. 围圈报数
原题链接
简单
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
int t, n, k;
struct node {
int v;
node* next;
};
void solve() {
cin >> t;
while (t--) {
k = 0;
cin >> n;
node* nd = new node();
node*dummy = nd;
for (int i = 1; i <= n - 1; i++) {
nd-> v = i;
nd -> next = new node();
nd = nd->next;
}
nd->v = n;
nd->next = dummy;
nd = nd->next;
while (nd->next != nd) {
k++;
if (k == 2) {
k = 0;
cout << (nd->next->v) << " ";
nd->next = nd->next->next;
}
nd = nd->next;
}
cout << (nd->v) << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
return 0;
}