题解 | #扑克牌大小#
扑克牌大小
https://www.nowcoder.com/practice/d290db02bacc4c40965ac31d16b1c3eb
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { String s = in.nextLine(); // 需要先替换大小王和2,因为大小王有JK,而Q为12 String line = s.replace("2", "15").replace("joker","16").replace("JOKER","17").replace("J", "11").replace("Q", "12").replace("K", "13").replace("A", "14"); String[] split = line.split("-"); String[] s1 = split[0].split(" "); String[] s2 = split[1].split(" "); String[] max = new String[]{"ERROR"}; // 有王炸 if (s1.length == 2 && split[0].replace("16", "") .replace(" ", "").equals("17")) { max = s1; } else if (s2.length == 2 && split[1].replace("16", "") .replace(" ", "").equals("17")) { max = s2; } else if (s1.length == 4 && split[0].replace(s1[0], "") .replace(" ", "").equals("")) { // 第一个手牌为炸弹 if (s2.length == 4 && split[1].replace(s2[0], "") .replace(" ", "").equals("") && Integer.parseInt(s1[0]) < Integer.parseInt(s2[0])) { // 第二个手牌也为炸弹,且大于第一个手牌 max = s2; } else { max = s1; } } else if (s2.length == 4 && split[1].replace(s2[0], "") .replace(" ", "").equals("")) { // 第二个手牌为炸弹(第一个手牌不为炸弹) max = s2; } else if (s1.length == s2.length && s1.length > 4) { // 顺子 // 排序去重 TreeSet<String> ts1 = new TreeSet<String>(); TreeSet<String> ts2 = new TreeSet<String>(); for (String str : s1) { ts1.add(str); } for (String str2 : s2) { ts2.add(str2); } if (ts1.size() == ts2.size()) { if (Integer.parseInt(ts1.last()) > Integer.parseInt(ts2.last())) { max = s1; } else { max = s2; } } } else if (s1.length == s2.length && split[0].replace(s1[0], "") .replace(" ", "").equals("") && split[1].replace(s2[0], "") .replace(" ", "").equals("")) { //单个 顺子 三个 if (Integer.parseInt(s1[0]) > Integer.parseInt(s2[0])) { max = s1; } else { max = s2; } } for (String result : max) { System.out.print(result.replace("11", "J").replace("12", "Q").replace("13", "K").replace("14", "A").replace("15", "2").replace("16","joker").replace("17","JOKER") + " "); } } } }