case analysis

We know that stacks and queues can implement many algorithms .

But as a beginner , Want to achieve those complex , Sophisticated algorithms are actually quite difficult .

Can we do some simple programs without using simple queues and stacks ?

Let's play a simple card game .

Now suppose there are players A And players B Play card games .

Suppose everyone has 6 Cards .

game player A And players B Take turns playing cards .

When a player plays the same card as the card on the table , You can put these two cards and the ones in the middle N Take the cards together and put them at the end of your hand .

When a player has no cards in his hand , Just lose .

Obviously , The cards on the table can be operated by stack ; The cards in hand can be realized by queues .

Module analysis

We know that we need to use two tools , But how to realize it ?
// Defining queues with structures struct queue { int data; int head; int tail; }; // Define stack with structure
struct stack { int data; int top; };
Basic operation completed , What's next ?
// With the structure , Of course, structural variables need to be defined struct queue q1,q2; struct stack s;
The next step is initialization
// We need an array of tags , Used to judge whether the cards are the same as those on the table int book={0};
End of initialization , Next, touch the cards ! We implement it by inputting !
for(int i=1;i<=6;i++) { scanf("%d",q1.data[q1.tail]); q1.tail++; } for(int
i=1;i<=6;i++) { scanf("%d",q2.data[q2.tail]); q2.tail++; }
Next comes the game !
if(book[t]==0) { q1.head++; s.top++; s.data[s.top]=t; book[t]=1; } else {
book[s.data[s.top]]=0; q1.data[q1.tail]=s.data[s.top]; q1.tail++; s.top--; }
book[s.data[s.top]]=0; q1.data[q1.tail]=s.data[s.top]; q1.tail++; s.top--; } }
game player B The way and players A In the same way , There is no repetition here .

The following is to judge the outcome of the game .
if(q1.head==q1.tail) { printf(" game player B win \n"); printf(" game player B The card in hand is :\n"); for(int
printf(" The cards on the table are :\n"); for(int i=1;i<=s.top;i++) printf("%d ",s.data[i]); } else
printf(" There are no cards on the table \n"); }
game player A How to win and players B The same way to win , I stopped writing because I was lazy .

Complete code
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> // Define a structure queue to simulate the cards in the player's hand
struct queue { int data; int head; int tail; }; // Define a structure stack to simulate cards on the table
struct stack { int data; int top; }; int main() { // Define structural variables struct queue
q1, q2; struct stack s; // Define an array of tags , Used to judge whether it is the same as the card on the table int book = { 0 };
// Initialize queues and stacks q1.head = 1; q1.tail = 1; q2.head = 1; q2.tail = 1; s.top = 0;
// use scanf Function simulation licensing for (int i = 1; i <= 6; i++) { scanf("%d", &q1.data[q1.tail]);
q1.tail++; } for (int i = 1; i <= 6; i++) { scanf("%d", &q2.data[q2.tail]);
q2.tail++; } // Simulated player playing cards int t = 0;// Define a temporary variable for playing cards while (q1.head < q1.tail &&
q2.head < q2.tail) { // Simulated player 1 Play cards t = q1.data[q1.head]; // When the card is not on the table if (book[t]
== 0) { // Leave the team q1.head++; // Put cards in the stack （ On the table ） s.top++; s.data[s.top] = t; book[t] = 1;
} // When there is this card on the table else { // Leave the team q1.head++; // Put the cards on the table at the end of the team q1.data[q1.tail] = t;
q1.tail++; // Keep playing cards according to the rules while (s.data[s.top] != t) { // Unmark book[s.data[s.top]] =
0; q1.data[q1.tail] = s.data[s.top]; q1.tail++; s.top--; } book[s.data[s.top]]
= 0; q1.data[q1.tail] = s.data[s.top]; q1.tail++; s.top--; } if (q1.head ==
q1.tail) break; // Simulated player 2 Play cards t = q2.data[q2.head]; // When the card is not on the table if (book[t] == 0)
{ // Leave the team q2.head++; // Put cards in the stack （ On the table ） s.top++; s.data[s.top] = t; book[t] = 1; }
// When there is this card on the table else { // Leave the team q2.head++; // Put the cards on the table at the end of the team q2.data[q2.tail] = t;
q2.tail++; // Keep playing cards according to the rules while (s.data[s.top] != t) { // Unmark 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] = s.data[s.top]; q2.tail++; s.top--; } } // Judge the outcome of the game if
(q2.head == q2.tail) { printf(" game player A win \n"); printf(" game player A The card in hand is \n"); for (int i =
q1.head; i <= q1.tail - 1; i++) printf("%d ", q1.data[i]); printf("\n"); if
(s.top > 0) { printf(" The cards on the table are \n"); for (int i = 1; i <= s.top; i++) printf("%d
", s.data[i]); } else printf(" There are no cards on the table \n"); } else { printf(" game player B win \n");
printf(" game player B The card in hand is \n"); for (int i = q2.head; i <= q2.tail - 1; i++) printf("%d
", q2.data[i]); printf("\n"); if (s.top > 0) { printf(" The cards on the table are \n"); for (int i =
1; i <= s.top; i++) printf("%d ", s.data[i]); } else printf(" There are no cards on the table \n"); }
return 0; }

Technology
Daily Recommendation