爱游戏攻略网

您现在的位置是:首页 > 手游攻略 > 用5789这四个数字算24点该怎么算?(用5788算24点怎么算)

用5789这四个数字算24点该怎么算?(用5788算24点怎么算)

小爱
一,用5789这四个数字算24点该怎么算?答案:(8-(9-5))*7思路:可以递归的方法来查找所有可能的组合。过程:fromitertoolsimportpermutations,productde....

用5789这四个数字算24点该怎么算?

一,用5789这四个数字算24点该怎么算?

答案:(8 - (9 - 5)) * 7

思路:可以递归的方法来查找所有可能的组合。

过程:

from itertools import permutations, product


def calculate(expression):

    try:

        return eval(expression)

    except ZeroDivisionError:

        return None


def solve24(numbers):

    operators = ['+', '-', '*', '/']

    for p in permutations(numbers):

        for ops in product(operators, repeat=3):

            expression = '(({} {} {}) {} {}) {} {}'.format(p[0], ops[0], p[1], ops[1], p[2], ops[2], p[3])

            result = calculate(expression)

            if result == 24:

                return expression

    return "No solution found."


numbers = [5, 7, 8, 9]

solution = solve24(numbers)

print(solution)

二,请用5678这四个数字算出24点综合算式

一般都是首先考虑那些简单就能得到24的,比如3*8, 6*4, 12*2这种运算,尽量凑这些数。这四个数里有8, 所以其他三个5,7,9可以去凑个3, (5+7-9)正好是3.

所以这十二就可以用,(5+7-9)*8得到。这四个数字不算特别奇特的,不需要用到一些奇特的运算,比如分数,开方啥的。

三,用数字57910算24点

(5+7-9)*8=24你可以再看看有没有其它的,有的有多种可能


总结:以上内容就是爱游戏攻略网提供的用5789这四个数字算24点该怎么算?(用5788算24点怎么算)详细介绍,大家可以参考一下。