【HDU 1276】士兵队列训练问题(两个队列模拟)
生活随笔
收集整理的這篇文章主要介紹了
【HDU 1276】士兵队列训练问题(两个队列模拟)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目鏈接
題目鏈接 http://acm.hdu.edu.cn/showproblem.php?pid=1276
解題思路
兩個隊列模擬即可,注意:
Code(G++)
#include <bits\stdc++.h>using namespace std; typedef long long ll; double eps = 1e-7;//ofstream out("out1.txt");int main() {int t;cin >> t;int n;while (t--) {cin >> n;queue<int> a, b;for (int i = 1; i <= n; ++i) {a.push(i);}//模擬操作,先2后3while (a.size() > 3 or b.size() > 3) {int cnt = 0;while (a.size()) {int x = a.front();a.pop();if (++cnt % 2 != 0) {b.push(x);}}if(b.size() <= 3) break;cnt = 0;while (b.size()) {int x = b.front();b.pop();if (++cnt % 3 != 0) {a.push(x);}}}//將余下的數(shù)字加入數(shù)組并排序輸出//a和b只可能有一個不為空int p[3];int cnt = 0;while (a.size()) {p[cnt++] = a.front();a.pop();}while (b.size()) {p[cnt++] = b.front();b.pop();}sort(p, p + cnt);for (int i = 0; i < cnt; ++i) {if (i != 0) cout << " ";cout << p[i];}cout << endl;}return 0; }總結
以上是生活随笔為你收集整理的【HDU 1276】士兵队列训练问题(两个队列模拟)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【HDU 3400】Line belt(
- 下一篇: 【POJ 1200】Crazy Sear