HDU 1276 士兵队列训练问题(队列)
2015-07-21 16:29
495 查看
HDU 1276 士兵队列训练问题
Problem Description某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报到三的出列,剩下的向小序号方向靠拢,继续从头开始进行一至二报数。。。,以后从头开始轮流进行一至二报数、一至三报数直到剩下的人数不超过三人为止。
[align=left]Input[/align]
本题有多个测试数据组,第一行为组数N,接着为N行新兵人数,新兵人数不超过5000。
[align=left]Output[/align]
共有N行,分别对应输入的新兵人数,每行输出剩下的新兵最初的编号,编号之间有一个空格。
[align=left]Sample Input[/align]
2
20
40
Sample Output
1 7 19 1 19 37
代码如下:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <queue> #include <algorithm> using namespace std; int main() { int n; while(scanf("%d",&n) != EOF) { while(n--) { int num; scanf("%d",&num); queue<int> q; for(int i = 1;i <= num;i++) q.push(i); while(q.size() > 3) { int len = q.size(); for(int i = 1;i <= len;i++) { if(i % 2 == 1) { int a = q.front(); q.push(a); q.pop(); } else { q.pop(); } } if(q.size() <= 3) break; len = q.size(); for(int i = 1;i <= len;i++) { if(i % 3 == 0) { q.pop(); } else { int a = q.front(); q.push(a); q.pop(); } } } while(q.size() > 1) { cout<<q.front()<<" "; q.pop(); } cout<<q.front()<<endl; } } return 0; } 99e4
相关文章推荐
- struts2、spring整合
- flash 调用启动一个.exe文件并向其传递参数的方法
- python中字符串的使用
- hdoj-1312-Red and Black
- linux 系统定时器的一些用法
- freeradius 启动报错Refusing to start with libssl version OpenSSL 1.0.1
- Android 网络通信框架Volley简介(Google IO 2013)
- Install Shield Limited Edition 下载、注册、安装 VS打包安装包
- poj2492解题报告
- HDOJ Air Raid 1151【最小路径覆盖】
- 给Ubuntu更换成163的源(sources.list)Unable to locate package
- 这是一篇测试文章
- 如果在文档已完成加载后执行 document.write,整个 HTML 页面将被覆盖
- java集合知识点概括
- OSI七层模型详解
- 关于JSON的一些数据格式转换
- IPTABLES进阶
- 一个之前没注意的小知识点1
- Android动态加载jar、apk的实现
- php json_encode() 数据