hdu 1276士兵队列问题【queue】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1276

                                           士兵队列训练问题

                                              Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                                                                 Total Submission(s): 11439    Accepted Submission(s): 5031


Problem Description
某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报到三的出列,剩下的向小序号方向靠拢,继续从头开始进行一至二报数。。。,以后从头开始轮流进行一至二报数、一至三报数直到剩下的人数不超过三人为止。
 


Input
本题有多个测试数据组,第一行为组数N,接着为N行新兵人数,新兵人数不超过5000。
 


Output
共有N行,分别对应输入的新兵人数,每行输出剩下的新兵最初的编号,编号之间有一个空格。
 


Sample Input
2
20
40
 
Sample Output
1 7 19
1 19 37
#include <stdio.h>             //已AC
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
int n, m;
queue<int>q;

int main()
{
    int i, j,cur1,cur2,b;
    scanf("%d", &n);
    while (n--)
    {
        while (!q.empty())q.pop();
        scanf("%d", &m);
        for (i = 1; i <= m; i++)
        {
            q.push(i);
        }
        int a = 1;
        while (q.size() > 3)
        {
            if (a % 2 == 1)
            {
                b = q.size();
                cur1 = q.size() / 2;
                for (i = 0; i < cur1; i++)
                {
                    q.push(q.front());
                    q.pop();
                    q.pop();
                }
                if ((b%2)!=0)
                {
                    q.push(q.front());
                    q.pop();
                }
            }
            else if (a % 2 == 0)
            {
                b = q.size();
                cur1 = q.size() / 3;
                for (i = 0; i < cur1; i++)
                {
                    q.push(q.front()); q.pop();
                    q.push(q.front()); q.pop();
                    q.pop();
                }
                b = b % 3;
                while (b--)
                {
                    q.push(q.front());
                    q.pop();
                }
            }
            a++;
        }
        if (q.size())
        {
            printf("%d", q.front());     //队列无迭代器,因为队列无表示最后一个元素的符号,如s.end();
            q.pop();
        }
        while (q.size())
        {
            printf(" %d", q.front());
            q.pop();
        }
        printf("
");
    }
    return 0;
}

2018-04-05


作者:is_ok
出处:http://www.cnblogs.com/00isok/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/00isok/p/8722800.html