题解 | #扑克牌大小#

扑克牌大小

https://www.nowcoder.com/practice/0a92c75f5d6b4db28fcfa3e65e5c9b3f

import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static int Sort(String s1, String s2) {
        if (s1.length() > 2) {
            if (s2.length() > 1) {
                if (s2.equals("JOKER")) {
                    return -1;
                }
                return 1;
            }
        }
        if (s2.length() > 2) {
            return -1;
        }
        String[] tmp = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        int a1 = 0;
        int a2 = 0;
        for (int i = 0; i < 13; i++) {
            if (a1 != 0 && a2 != 0) {
                break;
            }
            if (s1.equals(tmp[i])) {
                a1 = i;
            }
            if (s2.equals(tmp[i])) {
                a2 = i;
            }
        }
        return a1 - a2;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String str = in.nextLine();
            List<String> list1 = new ArrayList<>();
            List<String> list2 = new ArrayList<>();
            int i = 0;
            StringBuilder stringBuilder = new StringBuilder();
            while (i < str.length()) {
                char a = str.charAt(i);
                if (a == '-') {
                    list1.add(stringBuilder.toString());
                    stringBuilder = new StringBuilder();
                    i++;
                    break;
                } else if (a == ' ') {
                    list1.add(stringBuilder.toString());
                    stringBuilder = new StringBuilder();
                    i++;
                } else {
                    stringBuilder.append(a);
                    i++;
                }
            }
            while (i < str.length()) {
                char a = str.charAt(i);
                if (a == ' ') {
                    list2.add(stringBuilder.toString());
                    stringBuilder = new StringBuilder();
                    i++;
                } else {
                    stringBuilder.append(a);
                    i++;
                }
            }
            list2.add(stringBuilder.toString());
            int d1 = list1.size();
            int d2 = list2.size();
            if (d1 == d2) {
                if (d1 == 1) {
                    if (Sort(list1.get(0), list2.get(0)) > 0) {
                        System.out.print(list1.get(0));
                        continue;
                    } else if (Sort(list1.get(0), list2.get(0)) < 0) {
                        
                        System.out.print(list2.get(0));
                        continue;
                    }
                } else if (d1 == 2 || d1 == 4 || d1 == 5) {
                    if (Sort(list1.get(0), list2.get(0)) > 0) {
                        for (String a : list1) {
                            System.out.print(a + " ");
                        }
                        continue;
                    } else if (Sort(list1.get(0), list2.get(0)) < 0) {
                        for (String a : list2) {
                            System.out.print(a + " ");
                        }
                        continue;
                    }
                } else if (d1 == 3) {
                    for (int k = 0; k < 3; k++) {
                        if (Sort(list1.get(k), list2.get(k)) > 0) {
                            for (String a : list1) {
                                System.out.print(a + " ");
                            }
                            break;
                        } else if (Sort(list1.get(k), list2.get(k)) < 0) {
                            for (String a : list2) {
                                System.out.print(a + " ");
                            }
                            break;
                        }
                    }
                }
            } else {
                if (wang(list1)) {
                    for (String a : list1) {
                        System.out.print(a + " ");
                    }
                    continue;
                }
                if (wang(list2)) {
                    for (String a : list2) {
                        System.out.print(a + " ");
                    }
                    continue;
                }
                if (d1 == 4) {
                    int m = 0;
                    for (; m < 3; m++) {
                        if (!list1.get(m).equals(list1.get(m + 1))) {
                            System.out.println("ERROR");
                            break;
                        }
                    }
                    if (m == 3) {
                        for (String a : list1) {
                            System.out.print(a + " ");
                        }
                    
                    }
                    continue;
                    
                }
                if (d2 == 4) {
                    int m = 0;
                    for (; m < 3; m++) {
                        if (!list2.get(m).equals(list2.get(m + 1))) {
                            System.out.println("ERROR");
                            break;
                        }
                    }
                    if (m == 3) {
                        for (String a : list2) {
                            System.out.print(a + " ");
                        }
                    
                    }
                    continue;
                }
                System.out.println("ERROR");
            }
        }
    }

    public static boolean wang(List<String> list1) {
        if (list1.get(0).equals("JOKER") || list1.get(0).equals("joker")) {
            if (list1.get(1).equals("JOKER") || list1.get(1).equals("joker")) {
                return true;
            }
        }
        return false;
    }
}

全部评论

相关推荐

威猛的小饼干正在背八股:挂到根本不想整理
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
10-12 10:48
已编辑
秋招之苟:邻居家老哥19届双2硕大厂开发offer拿遍了,前几天向他请教秋招,他给我看他当年的简历,0实习实验室项目技术栈跟开发基本不沾边😂,我跟他说这个放在现在中厂简历都过不了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务