-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcatFishing2.c
More file actions
130 lines (116 loc) · 3.13 KB
/
Copy pathcatFishing2.c
File metadata and controls
130 lines (116 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct queue
{
int data[1000];
int head;
int tail;
};
struct stack
{
int data[10];
int top;
};
int main(int argc, char const *argv[])
{
struct queue q1, q2;
struct stack s;
int book[10];
int i, t;
q1.head = q1.tail = 0;
q2.head = q2.tail = 0;
s.top = -1;
for (i = 1; i <= 9; ++i) /// 记录桌上有哪些牌,初始为空
{
book[i] = 0;
}
for (i = 0; i < 6; ++i)
{
scanf("%d", &q1.data[q1.tail++]);
}
for (i = 0; i < 6; ++i)
{
scanf("%d", &q2.data[q2.tail++]);
}
while(q1.head < q1.tail && q2.head < q2.tail)
{
t = q1.data[q1.head]; /// 第一个人出第一张牌 出队
if(0 == book[t]) /// 桌上没有这张牌 入栈
{
s.data[++s.top] = t;
book[t] = 1; /// 并标记桌上已经有牌面为t的牌
q1.head++;
}
else
{
q1.data[q1.tail++] = t; /// 赢牌
q1.head++;
while(s.data[s.top] != t) /// 把相同的牌放到某人手中牌的末尾
{
book[s.data[s.top]] = 0;
q1.data[q1.tail++] = s.data[s.top--];
}
}
t = q2.data[q2.head];
if(0 == book[t])
{
s.data[++s.top] = t;
book[t] = 1;
q2.head++;
}
else
{
q2.data[q2.tail++] = t;
q2.head++;
while(s.data[s.top] != t)
{
book[s.data[s.top]] = 0;
q2.data[q2.tail++] = s.data[s.top--];
}
}
}
if(q2.head == q2.tail) /// 对手手中没有牌
{
printf("\nA win\n");
printf("A手中的纸牌: ");
for (i = q1.head; i < q1.tail; ++i)
{
printf("%d ", q1.data[i]);
}
if(s.top > -1) /// 输出桌上的牌
{
printf("\n桌上的牌: ");
for (i = 0; i <= s.top; ++i)
{
printf("%d ", s.data[i]);
}
}
else
{
printf("\n桌上已经没有纸牌了!");
}
}
else
{
printf("\nB win\n");
printf("B手中的纸牌: ");
for (i = q2.head; i < q2.tail; ++i)
{
printf("%d ", q2.data[i]);
}
if(s.top > -1)
{
printf("\n桌上的牌: ");
for (i = 0; i <= s.top; ++i)
{
printf("%d ", s.data[i]);
}
}
else
{
printf("\n桌上已经没有纸牌了!");
}
}
return 0;
}