【 UVA - 10935 】Throwing cards away I (卡片游戏) deque双端队列

题目链接

代码:

#include 
#include 
#include 
using namespace std;
int main()
{
	int n;
	while(cin>>n && n)
	{
		deque<int> q;
		for(int i=1;i<=n;i++) //先把1-n放入双端队列中
			q.push_back(i);
		cout<<"Discarded cards:";
		while(q.size()>1) //至少两个时
		{
			cout<<" "<<q.front(); 
			if(q.size()>2) cout<<",";
			q.pop_front(); //丢掉队首
			q.push_back(q.front()); //新的队首放队尾
			q.pop_front(); //新的队首丢掉
		}
		cout<<endl<<"Remaining card: "<<q.front()<<endl;
	}
   	return 0;
}

你可能感兴趣的:(POJ,+,HDU,+,UVA,队列,数据结构,算法,UVA,c++)