小猫钓鱼都知道怎么玩吧,那怎么用代码表示呢?其实用C++的队列和栈的知识很容易实现,下面展示一下我的代码(该代码规则是完全按照q1先出q2后出的规矩,不管是否收牌)

#include<iostream>
#include<stack>
#include<queue>
using namespace std;
int a[11];
int main()
{
	for(int i=1;i<=10;i++)
	{
		a[i]=0;
	}
	stack <int> s;
	queue <int> q1,q2;
	int n;
	cin>>n;
	int z,m,t;
	for(int i=1;i<=n;i++)
	{
		cin>>z;
		q1.push(z);
	}
	for(int i=1;i<=n;i++)
	{
		cin>>z;
		q2.push(z);
	}
	while(q1.size()!=0&&q2.size()!=0)
	{
		t=q1.front();
		if(a[t]==1)
		{
			q1.pop();
			q1.push(t);
			a[t]=0;
			while(s.top()!=t)
			{
				m=s.top();
				q1.push(m);
				s.pop(); 
				a[m]=0;
			}
			m=s.top();
			q1.push(m);
			s.pop();
			a[m]=0;
			
		}
		else
		{
			q1.pop();
			s.push(t);
			a[t]=1;
		}
		if(q1.empty()==1)
		{
			break;
		}
		t=q2.front();
		if(a[t]==1)
		{
			q2.pop();
			q2.push(t);
			a[t]=0;
			while(s.top()!=t)
			{
			    m=s.top();
				q2.push(m);
				s.pop();
				a[m]=0; 
				
			}
			m=s.top();
			q2.push(m);
			s.pop();
			a[m]=0;
			
		}
		else
		{
			q2.pop();
			s.push(t);
			a[t]=1;
		}
		
	}
	if(q1.size()!=0)
	{
		cout<<"q1获胜,手里的牌有"<<endl;
		for(int i=1;q1.empty()!=1;i++)
		{
			cout<<q1.front()<<endl;
			q1.pop();
		 } 
	}
	else
	{
		cout<<"q2获胜,手里的牌有"<<endl;
		for(int i=1;q2.empty()!=1;i++)
		{
			cout<<q2.front()<<endl;
			q2.pop();
		 } 
	}
	if(s.size()!=0)
	{
		cout<<"桌上的牌是:"<<endl;
		for(int i=1;s.empty()!=1;i++)
		{
			
			cout<<s.top()<<endl;
			s.pop();
		}
	}
	else
	{
		cout<<"桌上没有牌了"<<endl;
	}
	
	return 0;
	
	
}

哪里不对欢迎指教。

(祖国必然统一!!!)

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐