题解 | #扑克牌大小#
扑克牌大小
https://www.nowcoder.com/practice/d290db02bacc4c40965ac31d16b1c3eb
用哈希表存牌,然后讨论所有可能出现的情况。
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 获取左右俩边的牌并存入left和right中 String s = in.nextLine(); String[] s_split = new String[2]; s_split = s.split("-"); String left = s_split[0]; String right = s_split[1]; // 统计左右俩边几张牌并存入count_left和count_right中 int count_left = 1; int count_right = 1; for (int i = 0; i < left.length(); i++) { if (left.charAt(i) == ' ') count_left += 1; } for (int i = 0; i < right.length(); i++) { if (right.charAt(i) == ' ') count_right += 1; } //开一个map存牌用于比较大小 HashMap<String,Integer> map = new HashMap<>(); for(int i=3;i<=10;i++) map.put(String.valueOf(i),i); //存3-10 map.put("J",11); map.put("Q",12); map.put("K",13); map.put("A",14); map.put("2",15); map.put("joker",16); map.put("JOKER",17); //比较大小并输出结果 if (left.equals("joker JOKER") || left.equals("JOKER joker")) { System.out.println(left); // 判断左边是否为joker炸弹 } else if (right.equals("joker JOKER") || right.equals("JOKER joker")) { System.out.println(right); // 判断右边是否为joker炸弹 } else if (count_left != count_right && count_left != 4 && count_right != 4){ System.out.println("ERROR"); //判断是否可比较 } else if (count_left == 1) { //都是个子 if(map.get(left)>map.get(right)) System.out.println(left); else System.out.println(right); } else if (count_left == 2 || count_left == 3 || (count_left==4 && count_right==4)) { //都是对子,都是三个或者都是炸弹 String lf = ""; String rf = ""; if(left.substring(0,2).equals("10")) lf = "10"; else lf=left.substring(0,1); if(right.substring(0,2).equals("10")) lf = "10"; else rf = right.substring(0,1); if(map.get(lf)>map.get(rf)) System.out.println(left); else System.out.println(right); } else if (count_left == 4) { System.out.println(left); //只有left为炸弹 } else if (count_right == 4) { System.out.println(right); //只有right为炸弹 } else { //左右都是顺子 String[] lf = new String[5]; String[] rf = new String[5]; lf = left.split(" "); rf = right.split(" "); int min_left = 100; int min_right = 100; for(int i=0;i<5;i++){ if(map.get(lf[i])<min_left) min_left = map.get(lf[i]); if(map.get(rf[i])<min_right) min_left = map.get(rf[i]); } if(min_left>min_right) System.out.println(left); else System.out.println(right); } } }