2015 Blue Bridge Cup - the type of card type

topic:

牌型种数
小明被劫持到X赌城,被迫与其他3人玩牌。
一副扑克牌(去掉大小王牌,共52张, 13种牌),均匀发给4个人,每个人13张。
这时,小明脑子里突然冒出一个问题:
	如果不考虑花色,只考虑点数,也不考虑自己得到的牌的先后顺序,
	自己手里能拿到的初始牌型组合一共有多少种呢?

Solution 1: This question is for violence through all the possible matches to solve, first with the following method to facilitate understanding of resolve, then search analogy DFS understand.

def flag(num):
    if num > 13: return True
    return False

if __name__ == '__main__':
    count = 0
    result = [0] * 13
    for a1 in range(5):
        result[0] = a1
        for a2 in range(5):
            result[1] = result[0] + a2
            for a3 in range(5):
                result[2] = result[1] + a3
                for a4 in range(5):
                    result[3] = result[2] + a4
                    if flag(result[3]):
                        break
                    for a5 in range(5):
                        result[4] = result[3] + a5
                        if flag(result[4]):
                            break
                        for a6 in range(5):
                            result[5] = result[4] + a6
                            if flag(result[5]):
                                break
                            for a7 in range(5):
                                result[6] = result[5] + a7
                                if flag(result[6]):
                                    break
                                for a8 in range(5):
                                    result[7] = result[6] + a8
                                    if flag(result[7]):
                                        break
                                    for a9 in range(5):
                                        result[8] = result[7] + a9
                                        if flag(result[8]):
                                            break
                                        for a10 in range(5):
                                            result[9] = result[8] + a10
                                            if flag(result[9]):
                                                break
                                            for a11 in range(5):
                                                result[10] = result[9] + a11
                                                if flag(result[10]):
                                                    break
                                                for a12 in range(5):
                                                    result[11] = result[10] + a12
                                                    if flag(result[11]):
                                                        break
                                                    for a13 in range(5):
                                                        result[12] = result[11] + a13
                                                        if flag(result[12]):
                                                            break
                                                        if result[12] == 13:
                                                            count += 1
                                                            print(count)
    print(count)

Function flag (): used to ensure that the number of cards in less than 13
List result: Take a few cards used to record a
problem-solving ideas: Take 0-4 each card Zhang, if the last number of cards equal to 13, then considered a case
Solution 2: DFS search

count = 0

def f(k, cnt):
    """
    :param k: 拿到几种牌
    :param cnt: 拿到几张牌
    :return:
    """
    if k > 13 or cnt > 13:
        return
    if cnt == 13:
        global count
        count += 1
        return

    for i in range(5):
        f(k + 1, cnt + i)


if __name__ == '__main__':
    f(0, 0)
    print(count)

Compared to the first solution, this approach is a lot less code, but the principle is similar. Then the first solution to understand the analogy.

First, f function, k represents a parameter to get several cards, 13 corresponding to the first cycle of A Solution
Next, cnt parameter indicates to get a few cards, a first solution corresponding to the result list

Published an original article · won praise 0 · Views 30

Guess you like

Origin blog.csdn.net/CAG55688/article/details/104680606
Recommended