这题是小时候打牌时玩的“接竹竿”。如果出牌时局面已有相同的,那么收牌,再出,直到没有牌了或者新出的牌不在局面中,然后该对手出。
import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; import java.util.Stack; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); boolean[] exist = new boolean[14]; // 记录棋牌是否在桌面上 Queue<Integer> player1 = new LinkedList<>(); // 存玩家1的牌 Queue<Integer> player2 = new LinkedList<>(); // 存玩家2的牌 for (int i = 0; i < n; ++i) { player1.add(sc.nextInt()); } for (int i = 0; i < n; ++i) { player2.add(sc.nextInt()); } Stack<Integer> st = new Stack<>(); // 棋牌局面 int sum1 = 0, sum2 = 0; while(!player1.isEmpty() || !player2.isEmpty()) { sum1 += play(st, player1, exist); // 玩家1 出牌 sum2 += play(st, player2, exist); // 玩家2 出牌 } // 剩余牌 while (!st.isEmpty()) { if ((st.pop() & 1) == 1) { sum1++; } else { sum2++; } } System.out.println(sum1 + " " + sum2); } static int play(Stack<Integer> st, Queue<Integer> player, boolean[] exist) { int sum = 0; if (!player.isEmpty()) { while (!player.isEmpty() && exist[player.peek()]) { int val = player.poll(); sum++; while(st.peek() != val) { int topVal = st.pop(); sum++; exist[topVal] = false; } exist[st.pop()] = false; sum++; } if (!player.isEmpty()) { int val = player.poll(); exist[val] = true; st.push(val); } } return sum; } }
全部评论
(0) 回帖