题解 | #扑克牌大小#

扑克牌大小

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

import java.util.Scanner;
import java.util.*;
// 繁杂的业务处理题,伤神又伤身,t_t
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) { 
            String poker = in.nextLine();
            System.out.println(compare(poker));
        }
    }
    public static String compare(String poker) {
        String[] s = poker.split("-");
        String poker1 = s[0], poker2 = s[1];
        int[] p1 = change(poker1), p2 = change(poker2);
        if (p1[0] == p2[0]) {
            return p1[1] > p2[1] ? poker1 : poker2;
        }  else if (p1[0] == 6 || p1[0] == 4 && p2[0] != 4 && p2[0] != 6) {
            return poker1;
        } else if (p2[0] == 6 || p2[0] == 4 && p1[0] != 4 && p1[0] != 6) {
            return poker2;
        } else {
            return "ERROR";
        }
    }

    public static int[] change(String poker_) {
        //type: 1 一个或多个单个, 2 对子, 3 三个, 4 炸弹, 5 顺子, 6 王炸
        String[] poker = poker_.split(" ");
        int type[] = new int[2];
        int n = poker.length;
        if (n == 1) {
            type[0] = 1;
            type[1] = map.get(poker[0]);
        } else if (n == 2) {
            if (poker[0].equals(poker[1])) {
                type[0] = 2;
                type[1] = map.get(poker[1]);
            } else if (poker[0].equals("JOKER") && poker[1].equals("joker")
                       || poker[1].equals("JOKER") && poker[0].equals("joker")) {
                type[0] = 6;
                type[1] = 16;
            } else {
                int max = map.get(poker[0]) > map.get(poker[1]) ? map.get(poker[0])
                          : map.get(poker[1]);
                type[0] = 1;
                type[1] = max;
            }
        } else if (n == 3) {
            if (poker[0].equals(poker[1])
                    && poker[2].equals(poker[1])) {
                type[0] = 3;
                type[1] = map.get(poker[0]);
            } else {
                int pre = move(poker, 0, 3);
                type[0] = 1;
                type[1] = pre;
            }
        } else if (n == 4) {
            if (poker[0].equals(poker[1])
                    && poker[1].equals(poker[2])
                    && poker[2].equals(poker[3])) {
                type[0] = 4;
                type[1] = map.get(poker[0]);
            } else {
                int pre = move(poker, 0, 4);
                type[0] = 1;
                type[1] = pre;
            }
        } else if (n >= 5) {
            int pre = map.get(poker[0]);
            int off = 1;
            while (off < n) {
                if (map.get(poker[off]) == pre + 1) {
                    pre = map.get(poker[off]);
                    off++;
                } else {
                    break;
                }
            }
            if (off == n) {
                type[0] = 5;
                type[1] = map.get(poker[0]);
            } else {
                int pre2 = move(poker, 0, n);
                type[0] = 1;
                type[1] = pre2;
            }
        }
        return type;
    }
  
    public static move(String[] poker, int l, int r) {
        int pre = map.get(poker[l]);
            for (int i = 1; i < r; i++) {
               if (pre < map.get(poker[i])) {
                   pre = map.get(poker[i]);
               }
           }
	  return pre;
	} 
  
    static HashMap<String, Integer> map;
    static {
        map = new HashMap<String, Integer>();
        map.put("3", 1);
        map.put("4", 2);
        map.put("5", 3);
        map.put("6", 4);
        map.put("7", 5);
        map.put("8", 6);
        map.put("9", 7);
        map.put("10", 8);
        map.put("J", 9);
        map.put("Q", 10);
        map.put("K", 11);
        map.put("A", 12);
        map.put("2", 13);
        map.put("joker", 14);
        map.put("JOKER", 15);
    }
}

全部评论

相关推荐

zaakfung:26届不应该春招吗 为啥还实习
点赞 评论 收藏
分享
04-16 10:27
已编辑
美团_Saas_后端开发
今天周一休息,突发奇想写一篇阶段总结。如题,我已经去了一个和Java彻底毫无关联的行业。曾经我以为自己能在计算机行业发光发热,拿到美团offer那会感觉自己天都亮了。没想到刚入行一年多就当了逃兵。从最开始的热爱到现在一看到代码就厌恶,不知道自己经历了什么。所以我去干什么了?答案是:在成都当了租房销售。上班那会压力大了就念叨着去干租房中介,但是一直下不去这个决心,想着自己学了四年多的计算机知识,终究还是不甘心。终于在某一天准备八股文的时候,看着无数篇和工作内容关系不大的理论知识,那一刻下定决心,决定尝试一下销售行业,也算是给自己一个交代。后面阴差阳错的投了成都自如去当租房管家,没想到面试很顺利,在当天一百多个面试的人里面,我成为了为数不多通过的几个幸运儿之一。目前已经培训通过,正式入职,也开了单,有压力但是每天过得很开心,真心喜欢那种和人交流的感觉,哪怕是最后没有选择找我租房。说这些也是想告诉那些大三,大四正在找Java实习而焦虑的同学:你们现在还年轻,选择很多,容错率也很高,可以尽情去尝试自己喜欢的行业和工作。不用因为某一次的面试没通过或者简历石沉大海而焦虑,更不用因为身边人都在挤编程的独木桥就强迫自己跟风。也算是自己的碎碎念吧,也希望自己能在新的领域取得一点小成就。也祝牛油工作顺利!
沉淀小子:干啥都不丢人啊,生存是必须要的,销售很考验一个人综合素质能力的,好的销售人脉和资源可不比写字楼的白领差啊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务