C language implements cat fishing algorithm

  • 2020-06-15 09:47:12
  • OfStack

Cat fishing in C language realizes that two people play CARDS and put the CARDS on the table in turn. If the CARDS played by A are the same as those on the table, then A puts the two CARDS on the table and all the CARDS in the middle into the hands of A to see which of the two people play all the CARDS first.


#include <stdio.h>
struct queue
{
 int data[1000];
 int head;
 int tail;
};
struct stack
{
 int top;
 int data[10];
};
 
int main(){
 struct queue q1,q2;
 struct stack s;
 int i,t,r,flag=0;
 q1.head=1;q1.tail=1;
 q2.head=1;q2.tail=1;
 
 // Initialization table 
 s.top=0;
 // Read in 6 A card 
 for(i=1;i<=6;i++)
 {
 printf(" The input q1 the 6 CARDS: ");
 scanf("%d",&q1.data[i]);
 q1.tail++;
 }
 for(i=1;i<=6;i++)
 {
 printf(" The input q2 the 6 CARDS: ");
 scanf("%d",&q2.data[i]);
 q2.tail++;
 }
 // Out of the card 
 while(q1.head<q1.tail&&q2.head<q2.tail)
 {
 //debug
 printf("\nq1 The card in hand is :");
 for(i=q1.head;i<=q1.tail-1;i++)
 {
 printf(" %d",q1.data[i]);
 }
 printf("\nq2 The card in hand is :");
 for(i=q2.head;i<=q2.tail-1;i++)
 {
 printf(" %d",q2.data[i]);
 }
 if(s.top>0)
 {
 printf("\n The CARDS on the table are: ");
 for(i=1;i<=s.top;i++)
 {
 printf(" %d",s.data[i]);
 }
 printf("\n");
 }
 else
 {
 printf("\n There are no CARDS on the table \n");
 }
 //q1 Out of the card 
 flag=0;
 t=q1.data[q1.head];
 for(i=1;i<=s.top;i++)
 {
 if(t==s.data[i])
 {
 flag=1;break;
 }
 }
 if(flag==0)
 {
 q1.head++;
 s.top++;
 s.data[s.top]=t;
 }
 if(flag==1)
 {
 q1.head++;
 q1.data[q1.tail]=t;
 q1.tail++;
 while(s.data[s.top]!=t)
 {
 q1.data[q1.tail++]=s.data[s.top];
 s.top--;
 }
 q1.data[q1.tail]=t;
 q1.tail++;
 s.top--;
 }
 if(q1.head==q1.tail) break;
 //q2 Out of the card 
 flag=0;
 r=q2.data[q2.head];
 for(i=1;i<=s.top;i++)
 {
 if(r==s.data[i])
 {
 flag=1;break;
 }
 }
 if(flag==0)
 {
 q2.head++;
 s.top++;
 s.data[s.top]=r;
 }
 if(flag==1)
 {
 q2.head++;
 q2.data[q2.tail]=r;
 q2.tail++;
 while(s.data[s.top]!=r)
 {
 q2.data[q2.tail++]=s.data[s.top];
 s.top--;
 }
 q2.data[q2.tail]=r;
 q2.tail++;
 s.top--;
 } 
 }
 if(q1.head==q1.tail)
 {
 printf("q2 Win! ");
 printf("q2 The card in hand is :");
 for(i=q2.head;i<=q2.tail-1;i++)
 {
 printf(" %d",q2.data[i]);
 }
 if(s.top>0)
 {
 printf(" The CARDS on the table are: ");
 for(i=1;i<=s.top;i++)
 {
 printf(" %d",s.data[i]);
 }
 }
 else
 {
 printf(" There are no CARDS on the table ");
 }
 }
 if(q2.head==q2.tail)
 {
 printf("q1 Win! ");
 printf("q1 The card in hand is :");
 for(i=q1.head;i<=q1.tail-1;i++)
 {
 printf(" %d",q1.data[i]);
 }
 if(s.top>0)
 {
 printf(" The CARDS on the table are: ");
 for(i=1;i<=s.top;i++)
 {
 printf(" %d",s.data[i]);
 }
 }
 else
 {
 printf(" There are no CARDS on the table ");
 }
 }
 getchar();getchar();
}

Related articles: