【问题标题】:Python - Summing 'Aces' to provide the greatest sum less than 21Python - 求和“A”以提供小于 21 的最大总和
【发布时间】:2017-05-27 21:13:10
【问题描述】:

所以我在 python 中编写二十一点脚本,但遇到了手中的 A 的问题(有些手可能有 1 个 A,2 个 A 或击中后,3 个 A 或最多 4 个),问题是找到最大的和小于或等于 21。这听起来很容易,但是当每个 ace 的值可以是 1 或 11、不能重新计算并且 ace 的数量变化时,就会出现复杂情况,例如 p>

aces = [[1, 11], [1, 11]] #could be aces = [[1,11]] or etc. where one ace is [1, 11]

所以我目前所拥有的对我不起作用,因为 for 循环的数量已经固定了一定数量的 ace...(在本例中为两个)

possiblehand = []
for ace1 in aces:
    aces.remove(ace1) #To not re-count this current ace
    for ace2 in aces:
        handtotal = currenthandsum + ace1 + ace2 #two for-loops fixes two aces, but the amount of aces is varying. The error of adding lists exists also, but was not able get rid of this yet still add up all the combinations.
        if handtotal <= 21:
            possiblehand.append(handtotal)

hand = 0
for possible in possiblehand:
    if possible > hand:
        hand = possible  #just getting the greatest value in this list 

这被固定为手中正好有两个 A,并且存在我实际上添加两个列表的错误,但我想将 [1, 11] 的所有组合相加,以获得有多少个 A。 (例如,对于 20: 11, 1, 1, 7,其中 7 不是 ace)

那么我该如何解决这个问题呢?

【问题讨论】:

    标签: python addition blackjack


    【解决方案1】:

    你可以用一个二进制数来表示所有可能的手。

    假设我们手上有 3 张 A,所以有 23 = 8 种可能的手牌。我们可以用整数 0~7 来表示所有 8 种可能的手牌。

    假设我们想知道由整数 5 表示的可能手牌的值(即二进制形式的 101)。我们检查二进制整数的所有位,如果第 i 位是 1,我们考虑选择第​​ i 个 ace 为 11,否则我们考虑选择第​​ i 个 ace 为 1。然后可能手的值#5(或二进制形式的 101)是 11 + 1 + 11 + current_hand_value。

    ace_num = 3         # Or whatever number you like
    current_hand = 7    # Or whatever number you like
    ans = 0
    
    # Enumerate all the possible hands
    # Represent each possible hand by a binary number
    for i in range(0, 1 << ace_num):
        sum = current_hand
    
        for j in range(0, ace_num):
            # Decide to pick 1 or 11 according to the j-th bit of the binary number
            if ((j >> i) & 1) == 1:
                sum += 11
            else:
                sum += 1
    
        if sum > ans and sum <= 21:
            ans = sum
    
    print(ans)
    

    编辑

    其实这个问题还有另一种解决方案,我觉得更好。由于我们最多只能让手上的一张 ace 为 11(否则总和至少为 22,超过 21),我们只需要检查两种可能性:所有的 ace 都是 1,其中只有一个是 11 .

    ace_num = 3         # Or whatever number you like
    current_hand = 7    # Or whatever number you like
    ans = 0
    
    # All the aces are 1
    hand = ace_num + current_hand
    if hand <= 21 and hand > ans:
        ans = hand
    
    # Only one ace is 11
    if ace_num > 0:
        hand = ace_num-1 + 11 + current_hand
        if hand <= 21 and hand > ans:
            ans = hand
    
    print(ans)
    

    【讨论】:

    • 两个答案都非常聪明哈哈,第二个让我捂脸,奇怪我以前怎么没看到...
    猜你喜欢
    • 2019-08-07
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    • 2015-01-29
    • 2023-03-31
    • 1970-01-01
    • 2021-06-04
    • 2014-08-07
    相关资源
    最近更新 更多