#include<stdio.h>
struct quene//结构体的引入
{
int data[100];//
int head;
int tail;
};
struct desk//代表桌面的牌数
{
int data[1000];
int top;//栈顶
};
int main()
{
struct quene q1, q2;//结构体变量
struct desk s;
int book[100];//判断结束
int t, i;
q1.head = 1; q1.tail = 1;
q2.head = 1; q2.tail = 1;
s.top = 0;//初始化栈
for (i = 1; i <= 9; i++)//类比于桶排序
book[i] = 0;
for (i = 1; i <= 6; i++)
{
scanf_s("%d", &q1.data[q1.tail]);
q1.tail++;//最终 tail尾队列为7
}
for (i = 1; i <= 6; i++)
{
scanf_s("%d", &q2.data[q2.tail]);
q1.tail++;//最终 tail尾队列为7
}
while (q1.head < q1.tail && q2.head < q2.tail)//q1.head 是数值
{
t = q1.data[q1.head];//第一张
q1.head++;//无论如何 都还要出队
if (book[t] == 0)//还是没有t对应的牌 出队 标记 入栈
{
s.data[s.top] = t;//置入数组中才算入栈
book[t] =
1;
s.top++;
}
else
{
q1.data[q1.tail] = t;
q1.tail++;
for (; s.data[s.top] != t; s.top--)//从栈顶开始往下输出,知道找到栈中与打出的牌q1.head相同的值,停止
{
q1.data[q1.tail] = s.data[s.top];
q1.tail++;
//s.top--;
book[s.data[s.top]] = 0;//重置判断数组 必须为
}
}
t = q2.data[q2.head];//第一张
q2.head++;//无论如何 都还要出队
if (book[t] == 0)//还是没有t对应的牌 出队 标记 入栈
{
s.data[s.top] = t;//置入数组中才算入栈
book[t] =
1;
s.top++;
}
else
{
q2.data[q2.tail] = t;
q2.tail++;
for (; s.data[s.top] != t; s.top--)//从栈顶开始往下输出,知道找到栈中与打出的牌q2.head相同的值,停止
{
q2.data[q2.tail] = s.data[s.top];
q2.tail++;
//s.top--;
book[s.data[s.top]] = 0;//重置判断数组 必须将经过的每一个数组的下标全部重置为0
}
}
}
if (q2.head == q2.tail)
{
printf("q1 win");
}
if (q1.head == q1.tail)
{
printf("q2 win");
}
return 0;
}
自己对啊哈算法的一些思考,不过还是有点难懂