L1-019. 誰先倒(Java)PAT團體程序設計天梯賽-練習集

划拳是古老中國酒文化的一個有趣的組成部分。酒桌上兩人划拳的方法爲:每人口中喊出一個數字,同時用手比劃出一個數字。如果誰比劃出的數字正好等於兩人喊出的數字之和,誰就輸了,輸家罰一杯酒。兩人同贏或兩人同輸則繼續下一輪,直到唯一的贏家出現。
下面給出甲、乙兩人的酒量(最多能喝多少杯不倒)和划拳記錄,請你判斷兩個人誰先倒。

輸入格式:

輸入第一行先後給出甲、乙兩人的酒量(不超過100的非負整數),以空格分隔。下一行給出一個正整數N(<=100),隨後N行,每行給出一輪划拳的記錄,格式爲:

甲喊 甲劃 乙喊 乙劃
其中“喊”是喊出的數字,“劃”是劃出的數字,均爲不超過100的正整數(兩隻手一起劃)。

輸出格式:

在第一行中輸出先倒下的那個人:A代表甲,B代表乙。第二行中輸出沒倒的那個人喝了多少杯。題目保證有一個人倒下。注意程序處理到有人倒下就終止,後面的數據不必處理。

輸入樣例:

1 1
6
8 10 9 12
5 10 5 10
3 8 5 12
12 18 1 13
4 16 12 15
15 1 1 16

輸出樣例:

A
1


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] A = new int[2];   // A[0]-A的酒量,A[1]-A喝的杯數
        int[] B = new int[2];   // 同上
        A[0] = scanner.nextInt();
        B[0] = scanner.nextInt();

        int N = scanner.nextInt();
        while (N-- > 0) {
            int A_say = scanner.nextInt();      // A 喊的數字
            int A_hand = scanner.nextInt();     // A 劃的數字
            int B_say = scanner.nextInt();      // B 喊
            int B_hand = scanner.nextInt();     // B 劃

            // 如果 A 輸,B 贏
            if (A_say + B_say == A_hand && A_say + B_say != B_hand) {
                A[1]++;                         // A 喝的杯加一
                if (A[1] > A[0]) {              // 如果 A 喝的大於酒量就醉了
                    N = 0;  // break;           // 退出循環,後面數據不處理
                    System.out.println("A");    // 'A' 先倒了
                    System.out.println(B[1]);   // 輸出 B 喝的杯數
                }

            } else if (A_say + B_say == B_hand && A_say + B_say != A_hand) {
                // 如果 B 輸,A 贏,分析同上
                B[1]++;
                if (B[1] > B[0]) {
                    N = 0;  // break;
                    System.out.println("B");
                    System.out.println(A[1]);
                }

            } // 否則就是 兩個人同輸或同贏,繼續下一輪

        } // while

    } // main
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章