Stacks with queues, kitten-fishing-stacks and queues

Source: Internet
Author: User
Xiao Hum and Xiao ha play kitten fishing, first touch 6 cards, small hum first take the cards, and then out of the cards, if the card and the same on the table will be out of the cards placed at the end of the card, and will be placed between the beginning of the card, or the card to the table, ask who can win.
#include <cstdio> #include <queue> #include <stack> using namespace std;
     int main () {queue<int>q1;
     queue<int>q2;
     stack<int>s;
     int i,j,a,n,temp; int Vis[10]={0};//vis Array Save 1~9 card is not on the table (stack), it is impossible to appear in the stack of two identical cards, otherwise long been fished out for (i=1;i<=6;i++) {scanf ("%d", &am
         P;A);

     Q1.push (a);
         }//small hum take six cards for (i=1;i<=6;i++) {scanf ("%d", &a);

     Q2.push (a); }//Six card while ((!q1.empty ()) && (!q2.empty ())//small hum and small ha hand must have a card (stack is not empty), otherwise there is no card, the game is over {Temp=q1.fron
         T (); Q1.pop ()///No matter win the card must take out the card first (if wins the card must put in the last, loses the card to put on the table, anyway will not be in this ~ ~) if (vis[temp]==1)//If the table has this card {Q1.P
             Ush (temp);//First put this card into the Shang, the next is to take the cards on the table while (S.top ()!=temp)//Here note the size of the table for the temp card only may have a ~ ~, so as long as the top of the stack is not temp can {
         Q1.push (S.top ())//Take away the first card on the table and put it in the Shang S.pop () of the Little Hum; vis[temp]=0;//cancellation Mark, there is no temp card on the table ~ ~} if (vis[temp)==0)//If there is no card on the table, don't forget to put the card on the table (before it is because of winning the card) {S.push (temp);
         vis[temp]=1;//marked, temp table with}//above all for the operation of Small Hum temp=q2.front ();
         Q2.pop ();
             if (vis[temp]==1) {Q2.push (temp);
               while (S.top ()!=temp) {Q2.push (temp);
            S.pop ();
         } vis[temp]=0;
               } if (vis[temp]==0) {s.push (temp);
           Vis[temp]=1; } if (Q1.empty ()) printf ("Xiao Ha wins \ n");//Kha hands still have cards else printf ("Xiao Hum wins \ \")
;
 }


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.