题解 | #扑克牌大小#

扑克牌大小

https://www.nowcoder.com/practice/d290db02bacc4c40965ac31d16b1c3eb

import java.util.Scanner;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        Map<String, Integer> weightMap = new HashMap<>(13);
        weightMap.put("3", 1);
        weightMap.put("4", 2);
        weightMap.put("5", 3);
        weightMap.put("6", 4);
        weightMap.put("7", 5);
        weightMap.put("8", 6);
        weightMap.put("9", 7);
        weightMap.put("10", 8);
        weightMap.put("J", 9);
        weightMap.put("Q", 10);
        weightMap.put("K", 11);
        weightMap.put("A", 12);
        weightMap.put("2", 13);
        weightMap.put("joker", 14);
        weightMap.put("JOKER", 15);

        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String input = in.nextLine();
            String[]  cards = input.split("\\-");
            String[] cardA = cards[0].split(" ");
            String[] cardB = cards[1].split(" ");

            // 炸弹直接输出
            if (cards[0].equals("joker JOKER")) {
                System.out.println(cards[0]);
                continue;
            } else  if (cards[1].equals("joker JOKER")) {
                System.out.println(cards[1]);
                continue;
            }

            // 长度相等,则说明同类型。只需比较首位牌大小即可
            if (cardA.length == cardB.length) {
                if (weightMap.get(cardA[0])  > weightMap.get(cardB[0])) {
                    System.out.println(cards[0]);
                    continue;
                } else {
                    System.out.println(cards[1]);
                    continue;
                }
            }

            // 长度不相等
            // 若其中有一个为炸弹,则具有可比性,否则无可比性
            boolean aSame = cardA.length == 4 &&
                            cardA[0].equals(cardA[cardA.length - 1]);

            boolean bSame = cardB.length == 4 &&
                            cardB[0].equals(cardB[cardB.length - 1]);


            if ( aSame  || bSame ) {
                if (aSame) {
                    System.out.println(cards[0]);
                    continue;
                }

                if (bSame) {
                    System.out.println(cards[1]);
                    continue;
                }

            } else {
                System.out.println("ERROR");
            }
        }
    }
}

全部评论

相关推荐

喜欢走神的孤勇者练习时长两年半:池是池,发是发,我曾池,我现黑
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务