解题报告:hdu 1276 士兵队列训练问题 - 简单题
2024-08-22 23:40:13
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;
}
最新文章
- Jquery基本用法总结
- CoreData的使用
- Going from u to v or from v to u?_POJ2762强连通+并查集缩点+拓扑排序
- 在HCI层ACL Connection的建立
- ural 1114,计数dp
- HDMI接口与协议
- BestCoder Round #86 部分题解
- CentOS6.5_Nginx1.40_Php5.57_MySQL5.5.35编译安装全记录
- BZOJ Tyvj 1729 文艺平衡树
- jira汉化,破解,升级
- 新的方法 (New Approach)¶
- KISSY 库 demo
- Ruby设计模式透析之 —— 组合(Composite)
- grunt学习随笔
- 首次编译TI Android JB-4.2.2-DevKit-4.1.1的时候提示jdk版本不对
- centos下美团sql优化工具SQLAdvisor的安装
- Varnish后端主机的健康状态检查
- 宝藏(树形DP)
- 你真的知道.NET Framework中的阻塞队列BlockingCollection的妙用吗?
- oracle和mysql批量合并对比
热门文章
- 【Django错误】OSError: raw write() returned invalid length 14 (should have been between 0 and 7)
- Jacl 是 TCL 的一个备用实现
- Spark 2.2 DataFrame的一些算子操作
- PHP 基础篇 - PHP 中 DES 加解密详解
- Linux kernel AESNI缓冲区溢出漏洞
- 控制台应用程序中添加对MFC的支持
- HALCON串口通讯程序
- 聊一聊python的单例模式
- cpu与寄存器,内核态与用户态及如何切换
- mysql update 多表 (复制)