扑克游戏:
将一副扑克牌平均分成两份,每人拿一份。小哼先拿出手中的第一张扑克牌放在桌上,然后小哈也拿出手中的第一张放在桌上,就这样两人交替出牌。
出牌时,若某人打出的牌与桌面上的某张牌面相同,即将两张相同的牌以及中间的全部取走,并依次放在自己手中牌的末尾。当任意一人手中的牌全部取完时,游戏结束,对手获胜。
思路:
小哼和小哈有两种操作:出牌和赢牌,即对应出队和入队。桌子即相当于栈,打出一张牌和取走牌对应入栈和出栈
问题1
岂不是跟他相同的那个不会被收走
while(s.data[s.top]!=t) <---岂不是跟他相同的那个不会被收走
{
book[s.data[s.top]] = 0;//取消标记
q2.data[q2.tail] = s.data[s.top];
printf("收掉的牌: %d\n",s.data[s.top]) ;
q2.tail++;
s.top--;
}
问题2
用桶排序存数就把桌子上的数排序了取出来就不是原先的顺序了啊
for(i=1;i<=9;i++)
book[i]=0;//标记初始化,标记桌子上那些牌
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];
printf("收掉的牌: %d\n",s.data[s.top]) ;
q1.tail++;
s.top--;
}
#include<stdio.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;//标记初始化,标记桌子上那些牌
for(i=1;i<=6;i++)//向队列q1中插入6张牌
{
scanf("%d",&q1.data[q1.tail]);
q1.tail++;
}
for(i=1;i<=6;i++)//向队列q2中插入6张牌
{
scanf("%d",&q2.data[q2.tail]);
q2.tail++;
}
while(q1.tail>q1.head && q2.head<q2.tail)
{
t = q1.data[q1.head];//q1出牌
if(book[t]==0)//判断是否有牌可收
{
q1.head++;
s.top++;
s.data[s.top] = t;
book[t]=1;
printf("%d ",t);
for(i=1;i<=s.top;i++)
printf("桌子上的: %d ",s.data[i]);
printf("A %d\n",s.top);
}
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];
printf("收掉的牌: %d\n",s.data[s.top]) ;
q1.tail++;
s.top--;
}
}
t = q2.data[q2.head];//q1出牌
if(book[t]==0)//判断是否有牌可收
{
q2.head++;
s.top++;
s.data[s.top] = t;
book[t]=1;
printf("%d ",t);
for(i=1;i<=s.top;i++)
printf("桌子上的: %d ",s.data[i]);
printf("B %d\n",s.top);
}
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];
printf("收掉的牌: %d\n",s.data[s.top]) ;
q2.tail++;
s.top--;
}
}
}
if(q2.tail==q2.head)
{
printf("A win");
printf("A 当前牌:");
for(i=q1.head;i<=q1.tail-1;i++)
printf("%d",q1.data[i]);
if(s.top>0)
{
printf("桌子上的牌");
for(i=1;i<=s.top;i++)
printf("%d",s.data[i]);
}
else
printf("桌子上没牌啦!!");
}
else
{
printf("B win");
printf("B 当前牌:");
for(i=q2.head;i<=q2.tail-1;i++)
printf("%d",q2.data[i]);
if(s.top>0)
{
printf("桌子上的牌");
for(i=1;i<=s.top;i++)
printf("%d",s.data[i]);
}
else
printf("桌子上没牌啦!!");
}
return 0;
}