Java实现: 从一副扑克中随机抽取5张牌,判断是不是顺子(5张牌数字连续,大小王为任意数字)。

题目

从一副扑克中随机抽取5张牌,判断是不是顺子(5张牌数字连续,大小王为任意数字)。

代码

import java.util.Arrays;
import java.util.Random;

/**
 * Created by GuanDS on 2018/8/23.
 */
public class Check5Link {

    private static Random random = new Random(); // 随机数对象

    private static int[] array = new int[]{
            114, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
            214, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213,
            314, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313,
            414, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413,
            100, 200}; // 54张牌, A按14处理

    public static void main(String[] args) {
        int i = 0;
        while (i < 10) { // 打印10组顺子
            if (check()) {
                i++;
            }
        }
    }


    public static boolean check() {
        int[] check = checkout(5);
        int[] real = new int[5];
        for (int i = 0; i < check.length; i++) {
            real[i] = check[i] % 100;
        }
        // 按大小排序
        Arrays.sort(real);
        boolean isLink = true;
        int kingCount = 0;
        for (int i = 0; i < real.length; i++) { // 有重复的并且不是0的,或者包含2的都不是顺子
            if (real[i] == 2) {
                isLink = false;
                break;
            }
            if (real[i] == 0) {
                kingCount++;
            }
            if (i > 0 && real[i] == real[i - 1] && real[i] != 0) {
                isLink = false;
                break;
            }
        }

        // 判断间距
        if (real[real.length - 1] - real[kingCount] > real.length - 1) {
            isLink = false;
        }
        if (isLink) {
            StringBuffer sbCheck = new StringBuffer();
            StringBuffer sbLink = new StringBuffer();
            for (int i = 0; i < real.length; i++) {
                sbLink.append((real[i] < 10 ? " " + real[i] : (real[i] == 14 ? " A" : real[i])) + (i == real.length - 1 ? "" : ", "));
                sbCheck.append((check[i] < 10 ? " " + check[i] : check[i]) + (i == check.length - 1 ? "" : ", "));
            }
            System.out.println(sbCheck.toString() + "     " + sbLink.toString() + "-------" + (isLink ? "1" : "0"));
        }
        return isLink;
    }

    private static int[] checkout(int length) {
        int[] check = new int[length];
        int i = 0;
        while (i < length) {
            int sn = random.nextInt(array.length); // 随机序号
            boolean isContain = false;
            for (int k = 0; k < length && k < i + 1; k++) {
                if (check[k] == array[sn]) { // 重复抽取
                    isContain = true;
                    break;
                }
            }
            if (!isContain) {
                check[i] = array[sn];
                i++;
            }
        }
        return check;
    }

}

结果

211, 112, 210, 200, 208      0,  8, 10, 11, 12-------1
309, 210, 311, 408, 407      7,  8,  9, 10, 11-------1
310, 200, 111, 114, 412      0, 10, 11, 12,  A-------1
307, 100, 108, 309, 105      0,  5,  7,  8,  9-------1
205, 200, 106, 404, 107      0,  4,  5,  6,  7-------1
204, 105, 207, 203, 406      3,  4,  5,  6,  7-------1
105, 408, 307, 309, 406      5,  6,  7,  8,  9-------1
308, 409, 106, 105, 100      0,  5,  6,  8,  9-------1
204, 206, 107, 203, 105      3,  4,  5,  6,  7-------1
407, 206, 208, 410, 100      0,  6,  7,  8, 10-------1

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值