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

题目链接

代码:

#include <iostream>
#include <algorithm>
#include <queue>
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;
}

猜你喜欢

转载自blog.csdn.net/weixin_45260385/article/details/108182831
今日推荐