队列扑克游戏队列栈简单应用

#include<iostream>
using namespace std;
//分别建立队列和栈的结构体 
struct queue{
	int data[1000];
	int head;
	int tail;
};
struct stack{
	int data[1000];
	int top;
};//241256//313564
int main()
{
	int mark[10]={0};
	int a[6]={2,4,1,2,5,6};
	int b[6]={3,1,3,5,6,4};
	queue q1,q2;//定义队列 
	stack s;//定义栈
	//初始化队列和栈的节点 
	q1.head=1;
	q1.tail=1;
	q2.head=1;
	q2.tail=1;
	//初始化队列1装小哼的牌 
	for(int i=0;i<=5;i++)
	{
		q1.data[q1.tail++]=a[i]; 
	}
	//初始化队列2装小哈的牌 
	for(int i=0;i<=5;i++)
	{
		q2.data[q2.tail++]=b[i];
	}
	//栈顶初始值 
	s.top=0;
	//开始循环出牌,直到哪一方手里先没牌 
	while(q1.head!=q1.tail&&q2.head!=q2.tail)
	{
		//小哼出第一张牌放进桌面(栈内) 
		int t=q1.data[q1.head++]; 
		s.data[++s.top]=t;
		//判断是否有一样的牌 
		if(mark[t]==1) 
		{
			//有的话就循环把所有之间牌拿出来 
			q1.data[q1.tail++]=t;
			while(s.data[s.top]!=t)
			{
				mark[s.data[s.top]]=0;
				q1.data[q1.tail++]=s.data[s.top--];
			}
			mark[t]=0;
		}
		//小哼出牌结束之后要判断小哼出牌是否出完 
		if(q1.head!=q1.tail)
		{
			//还有牌就继续小哈出牌 
			t=q2.data[q2.head++];
			s.data[++s.top]=t;
			//判断是否有牌相等 
			if(mark[t]==1)
			{
				q2.data[q2.tail++]=t;
				//循环将所有之间的牌全部拿出 
				while(s.data[s.top]!=t)
				{
					mark[s.data[s.top]]=0;
					q2.data[q2.tail++]=s.data[s.top--];
				}
			}
			mark[t]=0;
		}
		else
		{
			break;
		}
	}
	//判断谁先空牌,谁先空谁输 
	if(q1.head==q1.tail)
	{
		cout<<"小哈胜"; 
	}
	else if(q2.head==q2.tail)
	{
		cout<<"小哼胜";
	}
	return 0;
}

STL实现

#include<iostream>
#include<queue>
#include<stack>
using namespace std;
int main()
{
	queue<int>q1;
	queue<int>q2;
	stack<int>s;
	int a[6]={2,4,1,2,5,6};
	int b[6]={3,1,3,5,6,4};
	int mark[9]={0};
	for(int i=0;i<=5;i++)
	{
		q1.push(a[i]);
	}
	for(int i=0;i<=5;i++)
	{
		q2.push(b[i]);
	}
	while(!q1.empty()&&!q2.empty())
	{
		int t=q1.front();
		q1.pop();
		s.push(t);
		if(mark[t]==1)
		{
			q1.push(t);
			while(s.top()!=t)
			{	
				mark[s.top()]=0;
				q1.push(s.top());
				s.pop();
			}
			mark[t]=0;
		}
		if(q1.empty())break;
		t=q2.front();
		q2.pop();
		s.push(t);
		if(mark[t]==1)
		{
			q2.push(t);
			while(s.top()!=t)
			{
				mark[s.top()]=0;
				q2.push(s.top());
				s.pop();
			}
			mark[t]=0;
		}
	}
	if(q1.empty())
	{
		cout<<"小哈胜";
	}
	else
	{
		cout<<"小哼胜";
	}
	return 0;
} 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值