卡片游戏UVa10935

https://cn.vjudge.net/contest/240999#problem/C

题目:桌上n张牌(n<=50),从第一张(位于顶面的牌)开始,从上往下依次编号为1~n,当至少剩下两张牌时:丢掉第一张,然后把新的第一张放到整叠牌最后,输入每行包含一个n,输出每次扔掉的牌以及最后剩下的牌。

方法:用队列中的k.pop();对第一张牌进行出队操作,然后k.push(k.front());拿第一张牌放到队尾,因为k.front并不会删去第一张牌,因此还要再用k.pop();使新的第一张从队首删去。

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <queue>
using namespace std;
queue<int> k;
int main(int argc, char *argv[])
{
	int n;
	while(cin>>n&&n)
	{
		for(int i=1;i<=n;i++)
		k.push(i);
		cout<<"Discarded cards:";
		while(k.size()!=1)
		{
			cout<<" "<<k.front();
			if(k.size()>2)
			cout<<",";
			k.pop();
			k.push(k.front());
			k.pop();
		}
		cout<<endl;
		cout<<"Remaining card: "<<k.front()<<endl;
		k.pop();
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/acm630/article/details/81355133
今日推荐