星期天小哼和小哈约在一起玩桌游,他们正在玩一个非常古怪的扑克游戏——“小猫钓鱼”。游戏的规则是这样的:将一副扑克牌平均分成两份,每人拿一份。小哼先拿出手中的第一张扑克牌放在桌上,然后小哈也拿出手中的第一张扑克牌,并放在小哼刚打出的扑克牌的上面,就像这样两人交替出牌。出牌时,如果某人打出的牌与桌上某张牌的牌面相同,即可将两张相同的牌及其中间所夹的牌全部取走,并依次放到自己手中牌的末尾。当任意一人手中的牌全部出完时,游戏结束,对手获胜
假如游戏开始时,小哼手中有6张牌,顺序为241256,小哈手中也有6张牌,顺序为3 13564,最终谁会获胜呢﹖
请你写一个程序来自动判断谁将获胜。这里我们做一个约定,小哼和小哈手中牌的牌面只有1~9。
#include <iostream>
using namespace std;
struct queue {
int data[1000];
int head;
int tail;
};
struct stack {
int data[10];
int top;
};
int main() {
queue q1,q2;
stack s;
int book[10];
int i,t;
//初始化队列
q1.head = 1;q1.tail = 1;
q2.head = 1;q2.tail = 1;
//初始化栈
s.top = 0;
//初始化用来标记哪些牌已经在桌上的数组
for (i = 1;1 < 10;i++)
book[i] = 0;
//小哼手上的6张牌
for (i = 1;i <= 6;i++) {
cin>>q1.data[q1.tail];
q1.tail++;
}
// 小哈手上的6张牌
for (i = 1;i <= 6;i++) {
cin>>q2.data[q2.tail];
q2.tail++;
}
//当队列不为空时执行循环
while (q1.head<q1.tail&&q2.head<q2.tail) {
t = q1.data[q1.head];//小哼出一张牌
//判断小哼当前打出的牌能否赢牌
if (book[t] == 0) {//表示桌面上没有牌面为t的牌
q1.head++;//把打出的牌出队
s.top++;
s.data[s.top] = t;//把打出的牌放到桌上,即入栈
book[t] = 1;
}
else {
q1.head++;
q1.data[q1.tail] = t;
q1.tail++;
//把桌上可以赢得的牌依次放到手中牌的末尾
while(s.data[s.top] != t) {
book[s.data[s.top]] = 0;
q1.data[q1.tail] = s.data[s.top];
q1.tail++;
s.top--;//栈中少了一张牌,所以栈顶要-1
}
}
t = q2.data[q2.head];//小哈出一张牌
//判断小哈当前打出的牌能否赢牌
if (book[t] == 0) {
q2.head++;
s.top++;
s.data[s.top] = t;
book[t] = 1;
}
else {
q2.head++;
q2.data[q2.tail] = t;
q2.tail++;
while (s.data[s.top] != t) {
book[s.data[s.top]] = 0;
q2.data[q2.tail] = s.data[s.top];
q2.tail++;
s.top--;
}
}
}
if (q2.head == q2.tail) {
cout<<"小哼win"<<endl;
cout<<"小哼当前手中的牌是";
for (i = q1.head;i<q1.tail;i++)
cout<<q1.data[i];
if(s.top > 0) {
cout<<"桌上的牌是";
for (i = 1;1 <= s.top;i++)
cout<<s.data[i];
}
else
cout<<"桌上已经没有牌了"<<endl;
}
else {
cout<<"小哈win"<<endl;
cout<<"小哈当前手中的牌是";
for (i = q2.head;i < q2.tail;i++)
cout<<q2.data[i];
if (s.top>0) {
cout<<"桌上的牌是";
for (i = 1;i <= s.top;i++)
cout<<s.data[i];
}
else
cout<<"桌上已经没有牌了"<<endl;
}
return 0;
}
请各位大牛帮帮忙,实在是看不出来了。。