华为面试题:五张牌,判断其属于什么类型

答案为:

public class TestStraightFlush {
    /**
     * @param args
     */
    public static void main(String[] args) throws IOException {
            Scanner sc = new Scanner(System.in);
            List<Card> cards=new ArrayList<>();
            for(int i=0;i<5;i++) {
            String cardS = sc.nextLine();
            String[] s=cardS.split(" ");
            Card card =new Card(s[0],s[1]);
            cards.add(card);
        }

        Collections.sort(cards, new Comparator<Card>(){
            /*
             * int compare(Student o1, Student o2) 返回一个基本类型的整型,
             * 返回负数表示:o1 小于o2,
             * 返回0 表示:o1和o2相等,
             * 返回正数表示:o1大于o2。
             */
            public int compare(Card o1, Card o2) {
                //按照金额大小进行降序排列
                if(o1.getNum() < o2.getNum()){
                    return 1;
                }
                if(o1.getNum() == o2.getNum()){
                    return 0;
                }
                return -1;
            }
        });

// TODO Auto-generated method stub
        if(isSF(cards)){
            System.out.println(1);
        }else if(isST(cards)){
            System.out.println(2);
        }else if(isHL(cards)){
            System.out.println(3);
        }else if(isSZ(cards)){
            System.out.println(5);
        } else if(isTH(cards)){
            System.out.println(4);
        }else if(isSTT(cards)){
            System.out.println(6);
        }else {
            System.out.println(7);
        }
    }

    private static boolean isSTT(List<Card> cards) {
        if(cards.get(0).getNum()==cards.get(2).getNum()){
            return true;
        }else if(cards.get(1).getNum()==cards.get(3).getNum()){
            return true;
        } else if(cards.get(2).getNum()==cards.get(4).getNum()){
            return true;
        }
        return false;
    }

    private static boolean isTH(List<Card> cards) {
        for(int i=0;i<cards.size()-1;i++) {
            if (!cards.get(i).getSuit().equals(cards.get(i + 1).getSuit())) {
                return false;
            }
        }
        return true;
    }

    private static boolean isSZ(List<Card> cards) {
        for(int i=0;i<cards.size()-1;i++) {
            if (cards.get(i).getNum() != cards.get(i + 1).getNum() - 1) {
                ;
                return false;
            }
        }
        return true;
    }

    private static boolean isHL(List<Card> cards) {
        if(cards.get(0).getNum()==cards.get(2).getNum()&cards.get(3).getNum()==cards.get(4).getNum()){
            return true;
        }else if(cards.get(0).getNum()==cards.get(1).getNum()&cards.get(2).getNum()==cards.get(4).getNum()){
            return true;
        }
        return false;
    }

    private static boolean isST(List<Card> cards) {
        if(cards.get(0).getNum()==cards.get(3).getNum()){
            return true;
        }else if(cards.get(1).getNum()==cards.get(4).getNum()){
            return true;
        }else {
            return false;
        }
    }

    // 判断是否同花顺
    static boolean isSF(List<Card> cards) {
// 判断花色
        String suit = cards.get(0).getSuit();
        int min = cards.get(0).getNum();
        int max = cards.get(0).getNum();
        for (int i = 1; i < cards.size(); i++) {
// 有花色异常则返回false
            if (!suit.equals(cards.get(i).getSuit()))
                return false;
//
            int num = cards.get(i).getNum();
            if (num > max)
                max = num;
            else if (num < min)
                min = num;
// 最后一张牌,确定max和min是所有牌的最大和最小
            if (i == cards.size() - 1)
//最大比最小大4,说明是同花顺
                return (max - min) == 4;
        }
        return true;
    }
}
// 扑克牌类
class Card {
    String suit;// 花色
    int num;// 大小
    public String getSuit() {
        return suit;
    }
    public void setSuit(String suit) {
        this.suit = suit;
    }
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
    public Card(String num, String suit) {
        super();
        this.suit = suit;
//1按照14理解
        if(num.equals("J"))
            this.num = 11;
        else if(num.equals("Q"))
            this.num = 12;
        else if(num.equals("K"))
            this.num = 13;
        else if(num.equals("A"))
            this.num = 14;
        else
            this.num = Integer.parseInt(num);
    }
}

猜你喜欢

转载自blog.csdn.net/wgangyiii/article/details/82154823
今日推荐