You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<bits/stdc++.h>
struct queue { //定义一个队的结构体 放两个人手中的牌
int data[1000];
int head;
int tail;
};
struct stack { //定义一个栈 放桌子上的牌
int data[10];
int top;
};
int main() {
struct queue q1,q2;
struct stack s;
int book[10];
int i,t;
//初始化队列
q1.head = 1;
q2.head = 1;
q1.tail = 1;
q2.tail = 1;
//初始化栈
s.top = 0;
//标记初始化,标记桌子上那些牌
for(i=1; i<=9; i++)
book[i]=0;
//向队列q1中插入6张牌
//小哼手上的6张牌
for(i=1; i<=6; i++) {
scanf("%d",&q1.data[q1.tail]);
q1.tail++;
}
//向队列q2中插入6张牌
//小哈手上的6张牌
for(i=1; i<=6; i++) {
scanf("%d",&q2.data[q2.tail]);
q2.tail++;
}
//当队列不为空的时候执行循环
while(q1.tail>q1.head && 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的牌
book[s.data[s.top]] = 0;//取消标记
q1.data[q1.tail] = s.data[s.top]; //依次放入队尾
q1.tail++;
s.top--;//栈中少了一张牌所以栈顶要减1
}
if(q1.head==q1.tail) break;//小哼手中的牌已打完,游戏结束
t = q2.data[q2.head];//小哈出一张牌
//判断小哈当前打出的牌是否能赢牌
if(book[t]==0) { //表明桌面上没有牌面为t的牌
//小哈本轮没有赢牌
q2.head++;//小哈打出一张牌,所以要把打出的牌出队
s.top++;
s.data[s.top] = t;//再把打出的牌放到桌上,即入栈
book[t]=1; //标记桌面上已有牌面为t的牌
} 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--;
}
//收回桌面上牌面为 t的牌
book[s.data[s.top]] = 0;//取消标记
q2.data[q2.tail] = s.data[s.top]; //依次放入队尾
q2.tail++;
s.top--;//栈中少了一张牌所以栈顶要减1
}
}
if(q2.tail==q2.head) {
printf("小哼 win\n");
printf("小哼当前手中的牌:");
for(i=q1.head; i<=q1.tail-1; i++)
printf("%d",q1.data[i]);
if(s.top>0) { //如果桌子上有牌,则依次输出桌子上的牌
printf("\n桌子上的牌");
for(i=1; i<=s.top; i++)
printf(" %d",s.data[i]);
} else
printf("\n桌子上没牌啦!!");
} else {
printf("小哈 win\n");
printf("小哈当前手中的牌:");
for(i=q2.head; i<=q2.tail-1; i++)
printf(" %d",q2.data[i]);
if(s.top>0) { //如果桌面上有牌就依次输出桌面上的牌
printf("\n桌子上的牌");
for(i=1; i<=s.top; i++)
printf(" %d",s.data[i]);
} else
printf("\n桌子上没牌啦!!");
}
return 0;
}