546C. Soldier and Cards

Source: Internet
Author: User

Topic links

Test instructions

Two people play poker, a total of n cards, the first person K1 Zhang, the second person K2 Zhang

The order in which the cards are given is the order of the cards.

Each time comparing the first two cards, the card above the number of win, the two cards to win the person, and the big cards placed in the bottom of the person's cards, the other one on top of the card, the other cards placed on the top of the two cards.

How many times do you want PK to end the game and record which person to win

If there is a dead loop output –1

This can be based on the stack or queue

Java program is based on the stack, the comparison time to remove the top of the stack, add a new two times, to first out of the stack, in the stack, a little trouble

The Python program is based on the queue, in the header out of the comparison, added to the end of the team to add elements, will not be excessive in the stack and out of the stack operation

Java has an increase in the queue implementation

ArrayList can realize the function of the queue, it is relatively simple

Java programs

Importjava.awt.List;ImportJava.io.PrintStream;Importjava.util.ArrayList;ImportJava.util.Scanner;ImportJava.util.Stack; Public classC546 {Static voidrun1 () {Scanner in=NewScanner (system.in); PrintStream out=System.out; intn =In.nextint (); intMaxTime =1000; ArrayList<Integer> L1 =NewArraylist<integer>(); ArrayList<Integer> L2 =NewArraylist<integer>(); intK1 =In.nextint ();  for(inti=1;i<=k1;i++) L1.add (In.nextint ()); intK2 =In.nextint ();  for(inti=1;i<=k2;i++) L2.add (In.nextint ()); intCount =0;  while(L1.size () >0 && l2.size () >0 &&maxtime>0) {Count++; MaxTime--; intQ1 = l1.get (0); intQ2 = L2.get (0); L1.remove (0); L2.remove (0); if(q1>Q2)                {L1.add (Q2);            L1.add (Q1); }Else{l2.add (Q1);            L2.add (Q2); }        }        if(maxtime==0) Out.println (-1); ElseOut.println (Count+ "" + (L1.size () >0?1:2)); }    Static voidrun () {Scanner in=NewScanner (system.in); PrintStream out=System.out; intn =In.nextint (); intK1 =In.nextint (); Stack S1=NewStack (); Stack S2=NewStack (); int[] A1 =New int[K1];  for(inti=0;i<k1;i++) A1[i]=In.nextint (); intK2 =In.nextint (); int[] A2 =New int[K2];  for(inti=0;i<k2;i++) {A2[i]=In.nextint (); }         for(inti=k1-1;i>=0;i--) S1.push (A1[i]);  for(inti=k2-1;i>=0;i--) {S2.push (a2[i]); }        intMaxTime = 10000; intCount = 0; BooleanFlag =false;  while(! S1.isempty () &&! s2.isempty () && maxtime>0){            intP1 =(Integer) S1.pop (); intP2 =(Integer) S2.pop (); if(p1<p2) {                int[] A3 =New int[S2.size () +2];  for(inti=0;i<a3.length-2;i++) A3[i]=(Integer) S2.pop (); A3[a3.length-2] =P1; A3[a3.length-1] = p2;//Max                 for(intI= a3.length-1;i>=0;i--) {S2.add (a3[i]); } Count++; }Else{                int[] A4 =New int[S1.size () +2];  for(inti=0;i<a4.length-2;i++) A4[i]=(Integer) S1.pop (); A4[a4.length-2] =P2; A4[a4.length-1] = p1;//Max                 for(intI= a4.length-1;i>=0;i--) S1.add (A4[i]); Count++; } maxtime--; }        if(maxtime==0) System.out.println (-1); Else if(S1.isempty ()) System.out.println (Count+ "" +2); ElseOut.println (Count+ "" + 1); }     Public Static voidMain (string[] args) {//run ();run1 (); }}

Python Program

Def C546 ():
n = Int (raw_input ())
L=lambda:map (Int,raw_input (). Split ())
A = L () [1:]
b = L () [1:]
c = 0
R = set ()
While A and B:
C +=1
A = A.pop (0)
B = B.pop (0)
If A > B:
A+=[b,a]
Else
B+=[A,B]
R = (tuple (a), tuple (b))
If R in R:
Print-1
Exit (0)
R.add (R)
Print C, 1 if a else 2

If __name__== ' __main__ ':
#A546 ()
#B546 ()
C546 ()

546C. Soldier and Cards

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.