HDU 1276 士兵隊列

有關隊列的應用,水題。可以創造兩個隊列,模擬報數。

#include<cstdio>
#include<iostream>
#include<queue>
#include<algorithm>

using namespace std;

queue<int> a,b;

int main()
{
	int n,t;
	cin>>n;
	while(n--) {
		cin>>t;
		for(int i=1;i<=t;i++) {
			a.push(i);
		}
		int num=0;

		while(a.size()>3) {
			while(!b.empty()) b.pop();
			
			if(num&1) {
				while(!a.empty()) {
					b.push(a.front()); a.pop();
					if(a.empty()) break; 
					b.push(a.front()); a.pop();
					if(a.empty()) break; 
					a.pop();
				}
			}
			else{
				while(!a.empty()) {
					b.push(a.front()); a.pop();
					if(a.empty()) break;
					a.pop();
				}
			}
			
			num^=1;
			swap(a,b);      //a.swap(b);因爲編譯器問題編不出來
		}
		
		cout<<a.front(); a.pop();
		while(!a.empty()) {
			cout<<' '<<a.front(); a.pop();
		}
		cout<<endl;
	}
	return 0;
}

//#include<queue>相關函數
//queue<T> q;
//q.empty(),q.size(),q.push(),q.front(),q.back(),q.pop(),q.emplace(),q.swap();

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章