题解 | #围圈报数# 队列解决 约瑟夫环问题
围圈报数
https://www.nowcoder.com/practice/b033062d346c4e42a7191b94164c1cd7
#include <iostream> #include <queue> using namespace std; int m, n; int main() { while(cin >> m) { while(m--) { cin >> n; queue<int> res; if(n == 1) printf("1"); else if(n == 2) printf("1 2"); else { int i; printf("3"); for(i = 4; i <= n; i++) res.push(i); res.push(1); res.push(2); i = 1; while(!res.empty()) { if(i % 3 == 0) { printf(" %d", res.front()); res.pop(); } else { res.push(res.front()); res.pop(); } i++; } } cout << endl; } } }