Huawei campus recruitment 2016 machine pen questions 3

Poker game we should all be more familiar with, the deck consisting of 54, including 3 ~ A, 2 each of 4, Wang 1, a king. Cards are small to large and the following character string represented by (wherein, represents a joker Wang lowercase, uppercase represents JOKER King) :) 

3 4 5 6 7 8 9 10 JQKA 2 joker JOKER
input two hands, between both hands with a "-" connected, each card hand of each separated by a space, "-" There are no spaces, such as: 4444 -joker JOKER
compare two hands the size of a large output card, if there is no comparison of the relationship between the output ERROR

basic rules:
(1) input of each hand may be tall, pair, straight (five consecutive), three bomb (four) and the king of one, there is no other case, the input to ensure that two hands are legitimate, straight already in ascending order;
(2) in addition to bombs and can be compared to the king and all the cards , other types of cards can only exist with the same type of comparison relationship (eg, pair with pair comparison, three with three comparison), removed the sign without considering the circumstances (such as: the child will be split into sub)
(3) the size of the common rules are rules and we usually know the same, tall, pairs, three comparisons card size; a straight comparison of the lowest card size; greater than all the bombs in front of cards, card size comparison between the bomb; the king is the largest brand;
hands (4) input The cards are equal does not appear.

The answer tips:
(1) In addition to bombs and outside of the king, must be compared with other types.
(2) input has been to ensure the legality, without checking whether the input is a legitimate card.
Junko (3) input has elapsed from small to large, and therefore do not have the sort.

Enter a description:

Input both hands, between both hands with "-" connected, each card hand of each separated by a space, "-" There are no spaces, such as the 4 4 ​​4 4-joker JOKER.

Output Description:

Output larger of two hands hands-free connectors, the same sequence of playing cards, still separated by a space; if there is no relationship between the comparator output ERROR.
Example 1

Entry

4 4 4 4-joker JOKER

Export

joker jOKER
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String line = scanner.nextLine();
            String[] towHands = line.trim().split("-");
            Poker left = create(towHands[0].trim().split(" "));
            Poker right = create(towHands[1].trim().split(" "));
            int result = left.compareTo(right);
            if (result > 0) {
                System.out.println(left.toString());
            } else if (result < 0) {
                System.out.println(right.toString());
            } else {
                System.out.println("ERROR");
            }
        }
    }


    private static Poker create(String[] items) {
        if (items.length == 1) {
            return new Single(items[0]);
        }

        if (items.length == 2) {
            if (items[0].equals("joker") || items[0].equals("JOKER")) {
                return new BigBoom(items);
            } else {
                return new Pair(items[0]);
            }
        }
        if (items.length == 3) {
            return new Three(items[0]);
        }
        if (items.length == 4) {
            return new Boom(items[0]);
        }

        if (items.length == 5) {
            return new Flow(items);
        }

        return null;
    }


    private enum Type {
        SINGLE,
        PAIR,
        FLOW,
        THREE,
        BOOM,
        BIGBOOM
    }


    private interface Poker {
        Type getType();

        int compareTo(Poker poker);
    }

    private static class Single implements Poker {
        private int value;
        private String name;

        public Single(String flag) {
            name = flag;
            value = Util.jugeValue(flag);
        }

        @Override
        public String toString() {
            return name;
        }

        public int compareTo(Poker poker) {
            if (poker.getType() == Type.BOOM ||
                    poker.getType() == Type.BIGBOOM) {
                return -1;
            }

            if (poker.getType() == Type.SINGLE) {
                Single other = (Single) poker;
                return value - other.value;
            }

            return 0;
        }

        public Type getType() {
            return Type.SINGLE;
        }
    }

    private static class Pair implements Poker {
        private int value;
        private String name;

        public Pair(String flag) {
            name = flag;
            value = Util.jugeValue(flag);
        }

        @Override
        public String toString() {
            return String.format("%s %s", name, name);
        }

        public int compareTo(Poker poker) {
            if (poker.getType() == Type.BOOM ||
                    poker.getType() == Type.BIGBOOM) {
                return -1;
            }

            if (poker.getType() == Type.PAIR) {
                Pair other = (Pair) poker;
                return value - other.value;
            }

            return 0;
        }

        public Type getType() {
            return Type.PAIR;
        }
    }

    private static class Three implements Poker {
        private int value;
        private String name;

        public Three(String flag) {
            name = flag;
            value = Util.jugeValue(flag);
        }

        @Override
        public String toString() {
            return String.format("%s %s %s", name, name, name);
        }

        public int compareTo(Poker poker) {
            if (poker.getType() == Type.BOOM ||
                    poker.getType() == Type.BIGBOOM) {
                return -1;
            }

            if (poker.getType() == Type.THREE) {
                Three other = (Three) poker;
                return value - other.value;
            }

            return 0;
        }

        public Type getType() {
            return Type.THREE;
        }
    }

    private static class Flow implements Poker {
        private int value;
        private String[] names;

        public Flow(String[] names) {
            this.names = names;
            value = Integer.parseInt(names[0]);
        }

        public int compareTo(Poker poker) {
            if (poker.getType() == Type.BOOM ||
                    poker.getType() == Type.BIGBOOM) {
                return -1;
            }

            if (poker.getType() == Type.FLOW) {
                Flow other = (Flow) poker;
                return value - other.value;
            }

            return 0;
        }

        @Override
        public String toString() {
            return String.join(" ", names);
        }

        public Type getType() {
            return Type.FLOW;
        }
    }

    private static class Util {
        public static int jugeValue(String flag) {
            if (flag.equals("JOKER")) {
                return 17;
            } else if (flag.equals("joker")) {
                return 16;
            } else if (flag.equals("2")) {
                return 15;
            } else if (flag.equals("A")) {
                return 14;
            } else if (flag.equals("K")) {
                return 13;
            } else if (flag.equals("Q")) {
                return 12;
            } else if (flag.equals("J")) {
                return 11;
            } else {
                return Integer.parseInt(flag);
            }
        }
    }

    private static class Boom implements Poker {
        private int value;
        private String name;

        public Boom(String flag) {
            name = flag;
            value = Util.jugeValue(flag);
        }


        @Override
        public String toString() {
            return String.format("%s %s %s %s", name, name, name, name);
        }

        public int compareTo(Poker poker) {
            if (poker.getType() == Type.BIGBOOM) {
                return -1;
            }

            if (poker.getType() == Type.BOOM) {
                Boom other = (Boom) poker;
                return value - other.value;
            }

            return 1;
        }

        public Type getType() {
            return Type.BOOM;
        }
    }

    private static class BigBoom implements Poker {
        private String[] names;

        public BigBoom(String[] names) {
            this.names = names;
        }

        public int compareTo(Poker poker) {
            return 1;
        }

        public Type getType() {
            return Type.BIGBOOM;
        }


        @Override
        public String toString() {
            return String.format("%s %s", names[0], names[1]);
        }
    }

}

 

Guess you like

Origin www.cnblogs.com/fly1024/p/12578206.html