#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;
}