题目链接:HDU 1276 士兵队列训练问题
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;
const int MOD = 10000007;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int maxn = 1010;
int a[maxn];
int main()
{
int t,n;
while(scanf("%d",&t)!=EOF)
{
while(t--)
{
scanf("%d",&n);
queue<int> q1,q2;
for(int i=1;i<=n;i++) //先都依次push进q1队列
q1.push(i);
while(1)
{
if(q1.size()<=3) //判断q1队列元素是否小于3
{
printf("%d",q1.front()); //按格式输出,末尾不能有空格
q1.pop();
while(!q1.empty())
{
printf(" %d",q1.front());
q1.pop();
}
break;
}
int cnt = 1;
while(!q1.empty()) //报一二,符合要求的push进q2
{
if(cnt%2!=0)
q2.push(q1.front());
cnt++;
q1.pop();
}
if(q2.size()<=3) //判断q2队列元素是否小于3
{
printf("%d",q2.front()); //按格式输出,末尾不能有空格
q2.pop();
while(!q2.empty())
{
printf(" %d",q2.front());
q2.pop();
}
break;
}
cnt = 1;
while(!q2.empty()) //报一二三,符合要求的push进q1
{
if(cnt%3!=0)
q1.push(q2.front());
cnt++;
q2.pop();
}
}
printf("\n");
while(!q1.empty()) //清空 q1 和 q2
q1.pop();
while(!q2.empty())
q2.pop();
}
}
return 0;
}