题解 | #扑克牌大小#

扑克牌大小

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.lang.System;

public class Main {
    
    private static String joker = "joker";
    private static String JOKER = "JOKER";
    
    private static class Info {
        //类型:0个子,1对子,2顺子,3三个,4炸弹,5对王
        Integer type;
        //值:
        //    个子(比如5)赋值自身,特殊值:J, Q, K分别是11, 12, 13,A赋值14,2赋值15,小王赋值100,大王赋值200
        //    对子(比如55)赋值自身
        //    顺子(比如34567)赋值最小,
        //    三个(比如555)赋值自身,
        //    炸弹(比如5555)赋值自身,
        //    对王不用赋值
        // 注意对子和对王都是2个
        Integer value;
    }
    
    //求最小值
    private static int getMin(String[] ss) {
        int min = Integer.parseInt(ss[0]);
        for (int i = 1; i < ss.length; i++) {
            int temp = getOneValue(ss[i]);
            if (temp < min) {
                min = temp;
            }
        }
        return min;
    }
    
    //由于只用比大小,不涉及计算,所以只要给的值符合相对大小即可
    //个子(比如5)赋值自身,特殊值:J, Q, K分别是11, 12, 13,A赋值14,2赋值15,小王赋值100,大王赋值200
    private static int getOneValue(String s) {
        if (s.equals("J")) {
            return 11;
        }
        if (s.equals("Q")) {
            return 12;
        }
        if (s.equals("K")) {
            return 13;
        }
        if (s.equals("A")) {
            return 14;
        }
        if (s.equals("2")) {
            return 15;
        }
        if (s.equals(joker)) {
            return 100;
        }
        if (s.equals(JOKER)) {
            return 200;
        }
        
        return Integer.parseInt(s);
    }
    
    //获取牌的类型
    //key: 0个子,1对子,2顺子,3三个,4炸弹,5对王
    //value: 个子(比如5)赋值自身,对子(比如55)赋值自身,顺子(比如34567)赋值最小,三个(比如555)赋值自身,
    //    炸弹(比如5555)赋值自身,对王不用赋值
    private static Info getTypeAndValue(String s) {
        String[] ss = s.split(" ");
        String str0 = ss[0];
        Info info = new Info();
        //个子
        if (ss.length == 1) {
            info.type = 0;
            //设置规定的对应值
            info.value = getOneValue(str0);
            return info;
        }
        //对子/对王
        if (ss.length == 2) {
            if (str0.equals(joker) || str0.equals(JOKER)) {
                info.type = 5;
                //对王可以不赋值,只根据类型判断。
            } else {
                info.type = 1;
                info.value = getOneValue(str0);
            }
            return info;
        }
        //顺子
        if (ss.length == 5) {
            info.type = 2;
            //求最小值
            info.value = getMin(ss);
            return info;
        }
        //三个
        if (ss.length == 3) {
            info.type = 3;
            info.value = getOneValue(str0);
            return info;
        }
        //炸弹
        if (ss.length == 4) {
            info.type = 4;
            info.value = getOneValue(str0);
            return info;
        }
        
        return info;
    }
    
    private static String getResult(String s0, String s1) {
        Info info0 = getTypeAndValue(s0);
        Info info1 = getTypeAndValue(s1);
        if (info0.type == null || info1.type == null) {
            //不符合条件,随便输出一个错误信息
            return "BIG ERROR";
        }
        int type0 = info0.type;
        int type1 = info1.type;
        //如果存在对王,则直接返回
        if (type0 == 5) {
            return s0;
        }
        if (type1 == 5) {
            return s1;
        }
        //如果存在炸弹,如果都是炸弹,返回较大的;否则返回是炸弹的那个
        //都是炸弹
        if (type0 == 4 && type1 == 4) {
            return info0.value > info1.value ? s0 : s1;
        }
        //返回是炸弹的那个
        if (type0 == 4) {
            return s0;
        }
        if (type1 == 4) {
            return s1;
        }
        
        //不存在对王和炸弹,则只能比较同类型的,如果类型不一致则返回ERROR
        if (type0 != type1) {
            return "ERROR";
        }
        
        //不为对王则上面的方法一定会赋值value
        if (info0.value == null || info1.value == null) {
            //不符合条件,随便输出一个错误信息
            return "BIG ERROR";
        }
        
        //分类型比较。
        //其实每个类型比较方法都一样,返回较大的值即可
        return info0.value > info1.value ? s0 : s1;
    }
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = br.readLine();
        String[] ss = s.split("-");
        System.out.println(getResult(ss[0], ss[1]));
    }
}

#华为机试#
全部评论

相关推荐

头像
02-15 16:23
中南大学 Java
野猪不是猪🐗:签了美团真是不一样! 亲戚们都知道我签了美团,过年都围着我问送一单多少钱,还让弟弟妹妹们引以为戒,笑我爸我妈养了个🐢孩子,说从小就知道我这个人以后肯定没出息,我被骂的都快上天了
点赞 评论 收藏
分享
2024-12-25 09:09
四川师范大学 运营
想和你交朋友的潜伏者要冲国企:先去沃尔玛亲身感受标准化流程体系,一两年后再跳槽国内任何零售行业,可以有更大选择权吧?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务