24点算法

import itertools
def twentyfour(cards):
    '''史上最短计算24点代码'''
    results = []
    for nums in itertools.permutations(cards):  # 四个数
        for ops in itertools.product('+-*/', repeat=3):  # 三个运算符(可重复!)
            # 构造三种中缀表达式 (bsd)
            bds1 = '({0}{4}{1}){5}({2}{6}{3})'.format(*nums, *ops)  # (a+b)*(c-d)
            bds2 = '(({0}{4}{1}){5}{2}){6}{3}'.format(*nums, *ops)  # (a+b)*c-d
            bds3 = '{0}{4}({1}{5}({2}{6}{3}))'.format(*nums, *ops)  # a/(b-(c/d))

            for bds in [bds1, bds2, bds3]:  # 遍历
                try:
                    if abs(eval(bds) - 24.0) < 1e-10:  # eval函数
                        results.append(bds)
                except ZeroDivisionError:  # 零除错误!
                    continue
    return results

cards = [4,3,13,13]
print(twentyfour(cards))

  

原文地址:https://www.cnblogs.com/pythonClub/p/15087561.html