hdoj-1276-士兵队列训练问题(队列模拟)

本文介绍了一种新兵按特定规则报数并出队的算法实现。通过使用队列进行模拟,确保每轮报数后检查剩余人数,以决定是否继续报数流程。最终输出所有人的出队顺序。

题意:

新兵蛋子按照两种报数规则报数:

1.1212报数,2出队

2.123123报数,3出队

没报完一轮,检查人数,不大于3,over

略坑,必须每报完一轮检查人数,最初,按照12两种顺序报完检查人数,WA了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
queue<int> Q;
int main()
{
    int t;
    cin>>t;
    while (t--) {
        int n;
        cin>>n;
        for (int i=1; i<=n; i++) Q.push(i);
        while (Q.size() > 3) {
            int i;
            i = 1;
            Q.push(Q.front());
            Q.pop();
            while (Q.front() != 1) {
                i++;
                if (i % 2 == 0) {
                    Q.pop();
                } else {
                    Q.push(Q.front()), Q.pop();
                }
            }
            if (Q.size() <= 3) break;//每一轮数完,判断人数不够了就停止 
            i = 1;
            Q.push(Q.front());
            Q.pop();
            while (Q.front() != 1) {
                i++;
                if (i % 3 == 0) {
                    Q.pop();
                } else {
                    Q.push(Q.front()), Q.pop();
                }
            }
        }
        while (Q.front() != 1) {//调整顺序
            Q.push(Q.front()), Q.pop();
        }
        cout<<Q.front();//注意格式
        Q.pop();
        while (!Q.empty()) {
            cout<<" "<<Q.front();
            Q.pop();
        }
        cout<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/evidd/p/9018311.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值