The 8th Blue Bridge Cup Carriage JAVA Riot

Suppose the children participating in the game are A and B. When the game starts, the random card sequence they get is as follows:
Party A: [K, 8, X, K, A, 2, A, 9, 5, A]
Party B : [2, 7, K, 5, J, 5, Q, 6, K, 4]
where X represents "10" and we ignore the suit of the card.
Starting from A, both A and B take turns.
When it was his turn to play cards, he took one from the head of his card queue, put it on the table, and pressed it on the top card (if any).
In this example, the game process:
A out of K, B out of 2, A out of 8, B out of 7, A out of X, the sequence on the table at this time is:
K, 2, 8, 7 , X
when it is B's turn At that time, his card K is the same as the K in the sequence of cards on the table, so he wins the cards including the K and between the two Ks and puts them in the tail of his card. Note: For the convenience of operation, the order of putting cards is opposite to the order on the table.
At this time, the hands of both A and B are:
Party A: [K, A, 2, A, 9, 5, A]
Party B: [5, J, 5, Q, 6, K, 4, K , X, 7, 8, 2, K] The
winning side continues to play. That is, B went out with 5, A out of K, B out of J, A out of A, and B out of 5, and they won again.
5, K, J, A, 5At
this time, both sides have cards in hand:
Party A: [2, A, 9, 5, A]
Party B: [Q, 6, K, 4, K, X, 7, 8, 2, K, 5, A, J, K, 5]
Note: More often than not, the winning side cannot win all the cards on the table, but takes away the same card points and the middle part. But in any case, the winning party continues to play, and sometimes it is allowed to win as soon as the card is played.
When a party discards the last card in their hand but cannot win a card from the table, the game ends immediately.
In the case of the initial hand in this example, the last A will lose, and the last hand of B is:
9K2A62KAX58K57KJ5
The task of this problem is to know the initial order of the two parties, and calculate the order of the hand of the winning side at the end of the game . When the game cannot be ended, -1 is output.
The input is 2 rows and 2 strings, which represent the sequence of cards in the initial hands of both A and B.
The output is 1 row, 1 string, which means that A plays cards first, and the card sequence in the hand of the winning side last.
For example,
enter:
96J5A898QA
6278A7Q973
, and the program should output:
2J9A7QA6Q6889977. For
example,
enter:
25663K6X7448
J88A5KJXX45A
, the program should output:
6KAJ458KXAX885XJ645
stupid
. If you say: keep deleting the front, add it later ... If you use an array, you need to open a large , Please bless Buddha before the end of the game, will not use the edge of the array.
There is something saying:
Anyway, the string is not long, it is better to return a new string for each operation.
There is something quietly said:
I generally do n’t squeak. This is a typical queue structure. Dynamic arrays are the best, are n’t they? Make one yourself!

Supplementary knowledge: StringBuffer and StringBuilder classes in Java

When modifying strings, you need to use the StringBuffer and StringBuilder classes.

Unlike the String class, objects of the StringBuffer and StringBuilder classes can be modified multiple times, and no new unused objects are generated.

The StringBuilder class was introduced in Java 5. The biggest difference between it and StringBuffer is that the StringBuilder method is not thread-safe (cannot be accessed synchronously).

Because StringBuilder has a speed advantage over StringBuffer, it is recommended to use the StringBuilder class in most cases. However, if the application requires thread safety, you must use the StringBuffer class! !

Idea: To understand the meaning of the question, it is to constantly delete the cards on the table, reverse them, and collect them in the bag. After the reverse, the order of the cards on the table is restored until the winner is selected.

import java.util.Scanner;

public class Main {
	static StringBuffer a = new StringBuffer();       // a的牌
	static StringBuffer b = new StringBuffer();       // b的牌
	static StringBuffer c = new StringBuffer();       // 桌面上的牌
	static int t;                                     // 桌面上a的第一张或b的第一张牌的位置

	public static void main(String[] args) {
		Main  cp = new Main ();
		Scanner scanner = new Scanner(System.in);
		String a1 = scanner.next();                   // 输入a的牌                  
		String b1 = scanner.next();                   // 输入b的牌
		a.append(a1);                                 // 存入a
		b.append(b1);                                 // 存入b
		cp.acp();                                     // a出牌
	}

	private void acp() {                              // 定义方法,a出牌
		t = c.toString().indexOf(a.charAt(0));        // 桌面上a即将出的牌的位置,没有则t=-1
		c.append(a.charAt(0));                        // a将牌放到桌面上
		if (t != -1) {                                // 如果桌面上有a出的牌,即a可以收牌
			c.reverse();                              // 因为赢牌是反着收牌的,所以将桌面上的牌全部反转
			a.append(c.substring(0, c.length() - t)); // a收取原先桌面上牌的t到最后一张牌,(因为桌面反转,所以是反着收的)
			c.reverse();                              // 反转,将桌面复原
			c.delete(t, c.length());                  // 删除桌面上a赢走的牌(t到最后一张牌)
			a.deleteCharAt(0);                        // a删掉出的牌
			acp();                                    // a继续出牌
		} else {                                      // 如果桌面上没有a即将出的牌
			a.deleteCharAt(0);                        // a删掉出的牌
			if (a.length() == 0) {                    // a没牌结束游戏 否则b出牌
				f(b);                                 // 游戏结束,b是赢家
			} else {
				bcp();                                // 否则b出牌
			}
		}
	}

	private void bcp() {                              // 定义方法,b出牌,同a的道理一样
		t = c.toString().indexOf(b.charAt(0));        // 桌面上b即将出的牌的位置,没有则t=-1
		c.append(b.charAt(0));                        // b将牌放到桌面上
		if (t != -1) {                                // 如果桌面上有b出的牌,即b可以收牌
			c.reverse();                              // 反转
			b.append(c.substring(0, c.length() - t)); // b收牌
			c.reverse();                              // 反转,将桌面复原
			c.delete(t, c.length());                  // 桌面去掉b收的牌
			b.deleteCharAt(0);                        // b删掉出的牌
			bcp();                                    // b继续出牌
		} else {
			b.deleteCharAt(0);                        // b删掉出的牌
			if (b.length() == 0) {                    // b没牌结束游戏 否则a出牌
				f(a);                                 // 游戏结束,a是赢家
			} else {
				acp();                               // 否则a出牌
			}
		}
	}

	private void f(StringBuffer ans) {               // 游戏结束,输出赢家的牌
		System.out.println(ans);
	}
}

Small theater: This parting, understatement. This farewell, understatement.

Published 203 original articles · praised 149 · 30,000+ views

Guess you like

Origin blog.csdn.net/weixin_43771695/article/details/105447177
Recommended