Card Game-Kitten fishing (aha!) Algorithm

Source: Internet
Author: User

Problem Description:

Xiao-hum and Xiao-ha are playing a rather strange poker game--kitty fishing. Game rules: Divide a deck of cards evenly into two parts, no one points. Xiao Hum first took out the hands of the first card on the table, and then Xiao Ha also took out the hands of the first card, and placed in the little hum just played the top of the poker, two people alternately out of cards. When the card is played, if someone is playing the same card as a card on the table, you can take two identical cards and all the cards in the middle, and then put them in the end of the hand. When any one of the hands when the card is finished, the game is over, the other wins.


Analysis:

1, the game a total of two operations: Card and win.

The card is out of the team, winning is the queue.

2, the desktop is equivalent to a stack, the card into the stack, winning card is out of the stack.

3. Learn what cards are available on the desktop by enumerating.


Ideas:

1, define two queues Q1, Q2 means small hum, Kha hands of cards.

2, define a stack s to represent the desktop card

3. Define an array book to record the cards already appearing on the desktop

4, the beginning of the game Q1 team first team, judging the current is a card or win

4.1 If it is a card, then the stack

4.2 If it is a win, then the Q1 team first to the end of the team, and then the winning cards in turn on the queue

5, Q2 team first team, operation of the same 4

6, 4th, 5 steps repeated until the hands of any person without a card

7, the output of the hands of the winning hand, the output of the card at this time on the desktop

Code:

(This part of the code is provided in the book)


#include <stdio.h> struct queue{int data[1000];
    int head;
int hail;

};
    struct stack{int data[10];
int top;

};
    int main (int argc, const char * argv[]) {struct queue q1,q2;
    struct stack s; int book[10]={0};
    
    Used to mark which cards are already on the table int i,t;
    Initialize Queue q1.head=1;
    Q1.hail= 1;
    q2.head=1;
    q2.hail=1;
    
    Initialize stack s.top=0;
        Enter 6 numbers for (i=1;i<=6;i++) {scanf ("%d", &q1.data[q1.hail]) to the queue in turn;
    q1.hail++;
        } for (i=1;i<=6;i++) {scanf ("%d", &q2.data[q2.hail]);
    q2.hail++;  } while (Q1.head<q1.hail&&q2.head<q2.hail) {T=q1.data[q1.head]; Q1 play the first card//To determine whether Q1 is currently playing the cards can win if (book[t]==0)//Surface table on the card is not the cards {//q1 This round can not win the card q1.head++;/
            /q1 played a card, played the card out of the team s.top++; S.data[s.top]=t;//q1 played cards into the stack book[t]=1;//mark the cards have been played T} else {//q1 This round can win the card Q1.head ++;//q1 Play the first card q1.data[q1.hail]=t;//q1 played cards put to the end of the team q1.hail++;
                while (s.data[s.top]!=t)//will win the cards in turn queue {q1.data[q1.hail]=s.data[s.top];
                q1.hail++;
                s.top--;  book[s.data[s.top]]=0;//the card to be enqueued from the desktop}} t=q2.data[q2.head];//q2 the card if (book[t]==0)
            Q2 this round cannot win the card {q2.head++;
            s.top++;
            S.data[s.top]=q2.data[q2.head];
        Book[t]=1;
            } else {//q2 This round can win the card q2.data[q2.hail]=q2.head;
            q2.head++;
            q2.hail++;
                while (s.data[s.top]!=t) {q2.data[q2.hail]=s.data[s.top];
                q2.hail++;
                book[s.data[s.top]]=0;
            s.top--;
        }}} if (Q2.head==q2.hail) {printf ("Q1 win!\n");
        printf ("The cards in the hands of the Q1 are: \ n");
for (i=q1.head;i<q1.hail;i++) {            printf ("%d", q1.data[i]);
            } if (s.top>0)//{printf ("\ n" cards on the table is: \ n ");
            for (i=1;i<=s.top;i++) {printf ("%d", s.data[i]); }} else printf ("\ nthe table has no cards.")
    \ n ");
            } else if (q1.head==q1.hail) {printf ("Q2 win!\n");
            printf ("The cards in the hands of the Q2 are: \ n");
            for (i=q2.head;i<q2.hail;i++) {printf ("%d", q2.data[i]);
                } if (s.top>0)//{printf ("\ n" cards on the table is: \ n ");
                for (i=1;i<=s.top;i++) {printf ("%d", s.data[i]); }} else printf ("\ nthe table has no cards.")
        \ n ");
    } getchar ();
    GetChar ();
return 0;
 }






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.