Loading

AcWing 4400. 玩游戏

题目链接

https://www.acwing.com/problem/content/4403/

题目思路

约瑟夫环问题(详情百度)
因为数据范围小,所以可以使用队列来实现数数和淘汰操作,每次数数操作由出队再入队实现,而淘汰则是只出队.

题目代码

#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;
queue<int> q;
int n, k;

int main()
{
    cin >> n >> k;
    
    for(int i = 1; i <= n; i ++ ) q.push(i);
    
    while(k -- )
    {
        int a;
        cin >> a;
        a %= q.size();
        
        for(int i = 0; i < a; i ++ )
        {
            q.push(q.front());
            q.pop();
        }
        
        cout << q.front() << ' ';
        q.pop();
    }
    return 0;
}
posted @ 2022-04-17 10:36  vacilie  阅读(20)  评论(0)    收藏  举报