Problem Description

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

Input

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

Output

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

Sample Input

2

20

40

Sample Output

1 7 19

1 19 37

代码如下:

#include <iostream>
#include <cstdio>
#include <queue> using namespace std; int n;
queue<int> q[2]; void work()
{
bool before = 0;
bool now;
for(int i = 0; i < n ; i++)
{
q[before].push(i+1);
}
bool judge = 0;
while(1)
{
now = !before;
if(q[before].size() <= 3)
{
judge = before;
break;
}
int cnt = 0;
while(!q[before].empty())
{
cnt = cnt%2+1;
if(cnt == 2)
{
q[before].pop();
}
else
q[now].push(q[before].front()),q[before].pop();
} before = now;
now = !before;
if(q[before].size() <= 3)
{
judge = before;
break;
}
int b = q[before].size();
cnt = 0;
while(!q[before].empty())
{
cnt = cnt%3+1;
if(cnt == 3)
{
q[before].pop();
}
else
q[now].push(q[before].front()),q[before].pop();
}
before = now; }
bool ff = false;
while(!q[judge].empty())
{
if(ff)
printf(" ");
printf("%d",q[judge].front());
q[judge].pop();
ff = true;
}
printf("\n");
return ;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
while(!q[0].empty())
q[0].pop();
while(!q[1].empty())
q[1].pop();
work();
}
return 0;
}

最新文章

  1. Jquery基本用法总结
  2. CoreData的使用
  3. Going from u to v or from v to u?_POJ2762强连通+并查集缩点+拓扑排序
  4. 在HCI层ACL Connection的建立
  5. ural 1114,计数dp
  6. HDMI接口与协议
  7. BestCoder Round #86 部分题解
  8. CentOS6.5_Nginx1.40_Php5.57_MySQL5.5.35编译安装全记录
  9. BZOJ Tyvj 1729 文艺平衡树
  10. jira汉化,破解,升级
  11. 新的方法 (New Approach)¶
  12. KISSY 库 demo
  13. Ruby设计模式透析之 —— 组合(Composite)
  14. grunt学习随笔
  15. 首次编译TI Android JB-4.2.2-DevKit-4.1.1的时候提示jdk版本不对
  16. centos下美团sql优化工具SQLAdvisor的安装
  17. Varnish后端主机的健康状态检查
  18. 宝藏(树形DP)
  19. 你真的知道.NET Framework中的阻塞队列BlockingCollection的妙用吗?
  20. oracle和mysql批量合并对比

热门文章

  1. 【Django错误】OSError: raw write() returned invalid length 14 (should have been between 0 and 7)
  2. Jacl 是 TCL 的一个备用实现
  3. Spark 2.2 DataFrame的一些算子操作
  4. PHP 基础篇 - PHP 中 DES 加解密详解
  5. Linux kernel AESNI缓冲区溢出漏洞
  6. 控制台应用程序中添加对MFC的支持
  7. HALCON串口通讯程序
  8. 聊一聊python的单例模式
  9. cpu与寄存器,内核态与用户态及如何切换
  10. mysql update 多表 (复制)