约瑟夫问题,用队列解决

围圈报数

https://www.nowcoder.com/practice/b033062d346c4e42a7191b94164c1cd7

#include<bits/stdc++.h>
using namespace std;
void func(int n) {
    queue<int> myq;
    for (int i = 1; i <= n; i++) myq.push(i);
    for (int i = 0; i < n; i++) {
        //get two num
        int t1 = myq.front();
        myq.pop();
        myq.push(t1);
        int t2 = myq.front();
        myq.pop();
        myq.push(t2);
        //print
        cout << myq.front() << ' ';
        myq.pop();
    }
}
int main() {
    int x;
    cin >> x;
    for (int i = 0; i < x; i++) {
        int m;
        cin >> m;
        func(m);
        cout << '\n';
    }

}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务