zoukankan      html  css  js  c++  java
  • 扑克牌的顺子问题

    题目:从扑克牌中随机抽5 张牌,判断是不是一个顺子,即这5 张牌是不是连续的。2-10 为数字本身,A 为1,J,为11,Q 为12,K 为13,而大小王可以看成任意数字。

    tip:将取出的5张牌排序并假定大小王为0,则会出现三种情况;

    1、5张牌中没有王,则5张牌的数字必须为连续,即间隔为0

    2、5张牌中有一个王,则允许出现间隔为1的情况

    3、5张牌中有两个王,则允许出现间隔为2的情况

    public static void main(String[] args) {
    	Random random = new Random();
    	int[] array = new int[5];
    	for (int i = 0; i < array.length; i++) {
    		array[i] = i + 10 * random.nextInt(1);
    	}
    	// 将数组排序,并假定大小王为0
    	Arrays.sort(array);
    	if (array[0] > 0) {// 5张牌中没有王
    		checkGap(array, 0, 4, 0);
    	} else if (array[0] == 0 && array[1] != 0) {// 5张牌有一个王
    		checkGap(array, 1, 4, 1);
    	} else {// 5张牌有两个王
    		checkGap(array, 2, 4, 2);
    	}
    }
    /**
    *allowGap,可能出现的间隔数
    */
    private static void checkGap(int[] array, int start, int end, int allowGap) {
    	int from = start;
    	while (from < end) {
    		allowGap -= array[from + 1] - array[from] - 1;
    		if (allowGap < 0) {
    			return;
    		}
    		from++;
    	}
    }
    

      

  • 相关阅读:
    jenkins GitHub 自动触发
    rabbitmq web管理
    系统编码,文件编码,python编码
    反转二叉树
    从右边看二叉树
    python pyenv
    js 闭包
    git review & devops过程
    centos7搭建自己的yum源
    优先级队列PriorityQueue 测试,会自动排序
  • 原文地址:https://www.cnblogs.com/cugb-2013/p/3662443.html
Copyright © 2011-2022 走看看