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;
}

 

posted @ 2018-05-10 10:25  朤尧  阅读(423)  评论(0编辑  收藏  举报