题解 | #扑克牌大小#
扑克牌大小
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])); } }
#华为机试#