tractor quiz

/*
 ============================================================================
 Name        : tractor.c
 Author      : Cly
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */


#include <stdio.h>
#include <stdlib.h>


typedef struct {              //手牌(队列)
int data[1000];
int head;
int tail;
}queue;


typedef struct {              //桌牌(堆栈)
int data[10];
int top;
}stack;


int main(void) {
//输入重定向
freopen("./in.txt","r",stdin);
queue q1, q2;
stack s;
int book[10];
int i, tmp;             //tmp为打出去的牌的临时存储
    //初始化队列
q1.head = 1, q1.tail = 1;
q2.head = 1, q2.tail = 1;
//初始化堆栈
s.top = 0;
//初始化标记数组
for (i = 0; i <= 9; ++i)
book[i] = 0;
    //初始化q1手牌
for (i = 1; i <= 6; ++i) {
scanf("%d", &q1.data[q1.tail]);
q1.tail++;
}
    //初始化q2手牌
for (i = 1; i <= 6; ++i) {
scanf("%d", &q2.data[q2.tail]);
q2.tail++;
}


while (q1.head < q1.tail && q2.head < q2.tail) {
tmp = q1.data[q1.head];     //q1先出牌
//判断q1是否能赢牌
if (book[tmp] == 0) {       //表示桌上没有tmp的牌面
//q1没有赢牌
q1.head++;              //q1打出一张牌
s.top++;
s.data[s.top] = tmp;    //入栈
book[tmp] = 1;          //标记桌上已有的牌面tmp
} else {
//q1此轮赢牌
q1.head++;              //q1打出一张牌
q1.data[q1.tail] = tmp; //打出的牌收入队尾
q1.tail++;
while (s.data[s.top] != tmp) {            //那桌上的牌依次放入到手中牌的队尾
book[s.data[s.top]] = 0;              //依次取消标记
q1.data[q1.tail] = s.data[s.top];     //依次放入队尾
q1.tail++;
s.top--;
}
//回收桌面上牌面为tmp的牌
book[s.data[s.top]] = 0;
q1.data[q1.tail] = tmp;
q1.tail++;
s.top--;
}
//q1手牌出完,Game Over~!
if (q1.head == q1.tail)
break;


tmp = q2.data[q2.head];       //q2出牌
if (book[tmp] == 0) {
q2.head++;
s.top++;
s.data[s.top] = tmp;
book[tmp] = 1;
} else {
q2.head++;
q2.data[q2.tail] = tmp;
q2.tail++;
while (s.data[s.top] != tmp) {
book[s.data[s.top]] = 0;
q2.data[q2.tail] = s.data[s.top];
q2.tail++;
s.top--;
}
book[s.data[s.top]] = 0;
q2.data[q2.tail] = tmp;
q2.tail++;
s.top--;
}
}
    //判定玩家胜负
if (q2.head == q2.tail) {
printf("q1 win!\n");
printf("q1=");
for (i = q1.head; i < q1.tail; i++)
printf("%d ", q1.data[i]);
if (s.top > 0) {                               //如果桌上有牌,依次输出
printf("\n The stack is:");
for (i = 1; i <= s.top; ++i)
printf("%d ", s.data[i]);
} else {
printf("\n The stack is Nothing.\n");
}
} else {
printf("q2 win!\n");
printf("q2:");
for (i = q2.head; i < q2.tail; ++i)
printf("%d ", q2.data[i]);
if (s.top > 0) {                               //如果桌上有牌,依次输出
printf("\n The stack is:");
for (i = 1; i <= s.top; ++i)
printf("%d ", s.data[i]);
} else {
printf("\n The stack is Nothing.\n");
}
}
    //关闭输入重定向
fclose(stdin);
return EXIT_SUCCESS;


getchar();getchar();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值