算法 | 输出扑克牌中的最大5连牌/6连牌/7连牌顺子 - Java实现

算法编程:输出扑克牌中的最大5连牌/6连牌/7连牌顺子

一. 题目描述:

输入13张扑克牌,在52张牌中产生(没有大王和小王),输出其中的最大5连牌/6连牌/7连牌顺子。

二.解题思路分析:

题目很简单,就是要输出手中13张牌中的最大5连牌/6连牌/7连牌顺子。有几点需要注意:

1.需要将J,Q,K,A分别转换为整数11,12,13,14,以便于进行整数排序;

2. 需要对13张牌进行排序,去重,涉及整形数组和字符串数组处理;

3. 需要用一个sequence累加器,遍历去重后的整形数组时,需要在每次判断

newInts[j + 1] - newInts[j] == 1时,sequence++;

4.对5连,6连,7连牌中第一个值与max5,max6,max7数组的第一个值进行比较,判断是否更新值。

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        //strs 字符串手牌  ints 整形手牌, ints2 排序后整形手牌, count[2-14] 各张牌的数量
        //求手中最大的顺子的小标  2:30-15:27

        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String[] strs = str.split(" ");
        int[] ints = new int[strs.length];

        int[] maxres5 = new int[5];
        int[] maxres6 = new int[6];
        int[] maxres7 = new int[7];
        int[] maxres8 = new int[8];

        //转换J,Q,K,A为数字11,12,13,14
        for (int i = 0; i < strs.length; i++) {
            if (strs[i].equals("2") || strs[i].equals("3") || strs[i].equals("4") || strs[i].equals("5") ||
                    strs[i].equals("6") || strs[i].equals("7") || strs[i].equals("8") || strs[i].equals("9") || strs[i].equals("10"))
                ints[i] = Integer.parseInt(strs[i]);
            else if (strs[i].equals("J")) ints[i] = Integer.parseInt("11");
            else if (strs[i].equals("Q")) ints[i] = Integer.parseInt("12");
            else if (strs[i].equals("K")) ints[i] = Integer.parseInt("13");
            else if (strs[i].equals("A")) ints[i] = Integer.parseInt("14");
        }

        //转换后的手牌进行排序
        int[] ints2 = ints;
        for (int i = 0; i < strs.length - 1; i++) {
            for (int j = i + 1; j < strs.length; j++) {
                if (ints2[i] > ints2[j]) {
                    int temp = ints2[i];
                    ints2[i] = ints2[j];
                    ints2[j] = temp;
                }
            }
        }
        //将重复出现的牌置换为Max_Value
        for (int i = 0; i < ints2.length; i++) {
            if (ints2[i] != Integer.MAX_VALUE) {
                for (int j = i + 1; j < ints2.length; j++) {
                    if (ints2[i] == ints2[j]) {
                        ints2[j] = Integer.MAX_VALUE;
                    }
                }
            }
        }
        String newStr = new String();
        newStr = "";
        //对去重后的数字串进行拼接
        for (int j = 0; j < ints2.length; j++) {
            if (ints2[j] != Integer.MAX_VALUE)
                newStr = newStr + ints2[j] + " ";
        }
        //split为字符串数组
        String[] strss = newStr.split(" ");

        //转换为整数数组
        int[] newInts = new int[strss.length];
        for (int j = 0; j < newInts.length; j++) {
            newInts[j] = Integer.parseInt(strss[j]);
        }
        int sequence = 0;
        //遍历整形数组,判断下一个整数和当前整数的差是否为1
        for (int j = 0; j < newInts.length-1; j++) {
           //如果为1, sequence累加器加一
            if (newInts[j + 1] - newInts[j] == 1) {
                sequence++;
            //否则,判断sequence是否大于等于4,即是否有5张连牌
            } else if (sequence >= 4) {
                //如果有,则输出这五张牌,用到k作为累加数组下标
                for (int k = 0; k <= sequence; k++) {
                    System.out.print(newInts[j - sequence + k] + " ");

                    //如果是5张牌,比较当前最大五连牌的起始值,更新手中最大五连牌的值
                    if (sequence == 4) {
                        if (newInts[j - sequence + k] > maxres5[0]) {
                            maxres5[k] = newInts[j - sequence + k];
                        }
                    }
                    //如果是6连牌,比较, 更新手中最大5,6连牌的值
                    if (sequence == 5) {
                        if (newInts[j - sequence + k] > maxres6[0]) {
                            maxres6[k] = newInts[j - sequence + k];
                            if (k < 5) {
                                maxres5[k] = newInts[j - sequence + k + 1];
                            }
                        }
                    }
                    //如果是7连牌,比较, 更新手中最大5,6,7连牌的值
                    if (sequence == 6) {
                        if (newInts[j - sequence + k] > maxres7[0]) {
                            maxres7[k] = newInts[j - sequence + k];
                            if (k < 6) {
                                maxres6[k] = newInts[j - sequence + k + 1];
                            }
                            if (k < 5)
                                maxres5[k] = newInts[j - sequence + k + 2];
                        }
                    }
                }
                //sequence清零
                sequence = 0;
            }
            //不是5连以上的牌就意味着断掉了,sequence清零
            else sequence = 0;
            //如果是最后末尾是连牌,sequence++完成,但没有输出,调用上面程序,再次刷新最大5,6,7连牌
            if (sequence >= 4) {
                for (int k = 0; k <= sequence; k++) {
                  //  System.out.print(newInts[j - sequence + k] + " ");

                    if (sequence == 4) {
                        if (newInts[j - sequence + k+1] > maxres5[0]) {
                            maxres5[k] = newInts[j - sequence + k+1];
                        }
                    }
                    if (sequence == 5) {
                        if (newInts[j - sequence + k+1] > maxres6[0]) {
                            maxres6[k] = newInts[j - sequence + k+1];
                            if (k < 5) {
                                maxres5[k] = newInts[j - sequence + k + 2];
                            }
                        }
                    }

                    if (sequence == 6) {
                        if (newInts[j - sequence + k+1] > maxres7[0]) {
                            maxres7[k] = newInts[j - sequence + k+1];
                            if (k < 6) {
                                maxres6[k] = newInts[j - sequence + k + 2];
                            }
                            if (k < 5)
                                maxres5[k] = newInts[j - sequence + k + 3];
                        }
                    }
                }
            }
        }

        //输出最大五连牌
        for (int i = 0; i < maxres5.length; i++) {
            System.out.print(maxres5[i] + " ");
        }
        System.out.println();
         //输出最大六连牌
        for (int i = 0; i < maxres6.length; i++) {
            System.out.print(maxres6[i] + " ");
        }

        System.out.println();
         输出最大七连牌
        for (int i = 0; i < maxres7.length; i++) {
            System.out.print(maxres7[i] + " ");
        }
    }
    }

你可能感兴趣的