题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1276
士兵队列训练问题
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11439 Accepted Submission(s): 5031
#include <stdio.h> //已AC #include <queue> #include <iostream> #include <algorithm> using namespace std; int n, m; queue<int>q; int main() { int i, j,cur1,cur2,b; scanf("%d", &n); while (n--) { while (!q.empty())q.pop(); scanf("%d", &m); for (i = 1; i <= m; i++) { q.push(i); } int a = 1; while (q.size() > 3) { if (a % 2 == 1) { b = q.size(); cur1 = q.size() / 2; for (i = 0; i < cur1; i++) { q.push(q.front()); q.pop(); q.pop(); } if ((b%2)!=0) { q.push(q.front()); q.pop(); } } else if (a % 2 == 0) { b = q.size(); cur1 = q.size() / 3; for (i = 0; i < cur1; i++) { q.push(q.front()); q.pop(); q.push(q.front()); q.pop(); q.pop(); } b = b % 3; while (b--) { q.push(q.front()); q.pop(); } } a++; } if (q.size()) { printf("%d", q.front()); //队列无迭代器,因为队列无表示最后一个元素的符号,如s.end(); q.pop(); } while (q.size()) { printf(" %d", q.front()); q.pop(); } printf("\n"); } return 0; }
2018-04-05