Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5430 Accepted Submission(s): 2500 Problem Description
某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报到三的出列,剩下的向小序号方向靠拢,继续从头开始进行一至二报数。。。,以后从头开始轮流进行一至二报数、一至三报数直到剩下的人数不超过三人为止。
Input
本题有多个测试数据组,第一行为组数N,接着为N行新兵人数,新兵人数不超过5000。
Output
共有N行,分别对应输入的新兵人数,每行输出剩下的新兵最初的编号,编号之间有一个空格。
Sample Input
2
20
40
Sample Output
1 7 19
1 19 37
Author
Cai Minglun
Source
杭电ACM集训队训练赛(VI)
我的思路是:暴力去消数字吧,反正最大只有5000,俩队列换来换去的就没了,注意输出格式就行。
代码如下:
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
int main()
{
int u;
int n;
scanf ("%d",&u);
while (u--)
{
queue<int>q0;
queue<int>q1;
scanf ("%d",&n);
for (int i = 1;i <= n;i++)
q0.push(i);
int op = 1; //op为1则报2离开,为0时则为报3离开
while (1)
{
if (op)
{
if (q0.size() <= 3)
{
while (q0.size() > 1)
{
printf ("%d ",q0.front());
q0.pop();
}
printf ("%d\n",q0.front());
break;
}
int num=1;
while (!q0.empty())
{
if (num == 1)
{
q1.push(q0.front());
q0.pop();
num = 2;
}
else
{
q0.pop();
num = 1;
}
}
op = 0;
}
else
{
if (q1.size() <= 3)
{
while (q1.size() > 1)
{
printf ("%d ",q1.front());
q1.pop();
}
printf ("%d\n",q1.front());
break;
}
int num=1;
while (!q1.empty())
{
if (num != 3)
{
q0.push(q1.front());
q1.pop();
num++;
}
else
{
q1.pop();
num = 1;
}
}
op = 1;
}
}
}
return 0;
}