21点和24点数学问题

21点问题

给定4个数字使用加、减、乘、除,小括号运算使等式等于21
参考解决方案:
1.全排列组合和笛卡尔积实现
2.迭代法实现

全排列组合和笛卡尔积实现

from itertools import permutations, product

# 记录运行的总次数
totalTimes: int = 0
# 结果集(除去重复的表达式)
expSet = set()
# 浮点数相等比较误差值
epsilon = 0.0000001

def solve_exp21(nums):
    if len(nums) != 4:
        print("Error:数字个数必须为4")
        return
    global totalTimes
    # 生成所有可能的排列和运算符组合
    for perm in permutations(nums):
        # print(perm)
        # 笛卡尔积(repeat=3?数字中间最多只有3个符号,可重复使用)
        for ops in product('+-*/', repeat=3):
            # d:digit o:ops
            fmt0 = "d" * 7
            fmt1 = "(dod)odod"
            fmt10 = "((dod)od)od"
            fmt11 = "(dod)o(dod)"
            fmt2 = "do(dod)od"
            fmt20 = "(do(dod))od"
            fmt21 = "do((dod)od)"
            fmt3 = "dodo(dod)"
            fmt30 = "(dod)o(dod)"
            fmt31 = "do(do(dod))"
            fmtlst = [fmt0, fmt1, fmt10, fmt11, fmt2, fmt20, fmt21, fmt3, fmt30, fmt31]
            for tmpFmt in fmtlst:
                totalTimes += 1
                tmpFmt = tmpFmt.replace("d","{}")
                tmpFmt = tmpFmt.replace("o","{}")
                tmpExp = tmpFmt.format(perm[0], ops[0], perm[1], ops[1], perm[2], ops[2], perm[3])
                value = eval(tmpExp)
                if abs(value - 21) < epsilon:
                    expSet.add(tmpExp)

参考结果:

totalTimes= 15360
[0] 7+(5*4)-6
[1] 7*(5+(4-6))
[2] 4*5-6+7
[3] 7-6+(5*4)
[4] 5*4-6+7
[5] 7*(4+(5-6))
[6] 7*((4-6)+5)
[7] 7*((5-6)+4)
[8] 6+5*(7-4)
[9] ((5*4)+7)-6
[10] 5*4+(7-6)
[11] (5*4)-6+7
[12] 6-(4-7)*5
[13] (7-6)+4*5
[14] (7-6)+5*4
[15] (5*(7-4))+6
[16] 7+(4*5)-6
[17] ((4+5)-6)*7
[18] ((4*5)-6)+7
[19] (4-(6-5))*7
[20] 6+(7-4)*5
[21] (7-6)+(5*4)
[22] 7*((5+4)-6)
[23] 6+(5*(7-4))
[24] (4*5)-6+7
[25] (4*5)+7-6
[26] 4*5+(7-6)
[27] (4+(5-6))*7
[28] 5*(7-4)+6
[29] 7+5*4-6
[30] 7*(5-(6-4))
[31] 5*4-(6-7)
[32] 4*5-(6-7)
[33] (4*5)-(6-7)
[34] ((5+4)-6)*7
[35] (7+(4*5))-6
[36] 7+((4*5)-6)
[37] 7+4*5-6
[38] 7*(4-(6-5))
[39] (7+(5*4))-6
[40] 7-(6-(4*5))
[41] 7-6+5*4
[42] 7+((5*4)-6)
[43] ((4*5)+7)-6
[44] (7-4)*5+6
[45] 7*((4+5)-6)
[46] 7-6+(4*5)
[47] 5*4+7-6
[48] 6-5*(4-7)
[49] 6-((4-7)*5)
[50] 7-6+4*5
[51] (5+(4-6))*7
[52] ((4-6)+5)*7
[53] 6-(5*(4-7))
[54] (4*5)+(7-6)
[55] (5-(6-4))*7
[56] (7-6)+(4*5)
[57] (5*4)-(6-7)
[58] 7-(6-(5*4))
[59] ((7-4)*5)+6
[60] (5*4)+7-6
[61] 4*5+7-6
[62] (5*4)+(7-6)
[63] 6+((7-4)*5)
[64] ((5-6)+4)*7
[65] ((5*4)-6)+7

24点问题

给定5个5数字使用 加、减、乘、除,小括号运算使等式等于24

迭代法实现

def solve24(dgs: list, numLen: int, exp: list):
    global totalTimes
    totalTimes += 1
    # 验证结果值
    if numLen == 1:
        boFound = abs(dgs[0] - 24) < epsilon
        if boFound:
            # 用求表达式的值函数验证结果
            real = eval(exp[0])
            print("boFound:", exp[0], dgs, f"real={real} {real==dgs[0]}")
            expSet.add(exp[0])
        return boFound

    # 分析表达式的每一个数字的+-*/是否可以得到目标值,且从第0,1,...个索引值开始两两计算并合并
    for i in range(numLen - 1):
        tmpDgs = []
        tmpExp = []
        # 分析加法能否满足条件(第i个值和第i+1个值)
        tmpV = dgs[i] + dgs[i + 1]
        tmpVExp = getExp(exp[i], exp[i + 1], "+")
        j = 0
        while j < numLen:
            if j in [i, i + 1]:
                tmpDgs.append(tmpV)
                tmpExp.append(tmpVExp)
                j += 1
            else:
                tmpDgs.append(dgs[j])
                tmpExp.append(f"{exp[j]}")
            j += 1
        if not solve24(tmpDgs, len(tmpDgs), tmpExp):
            tmpDgs.clear()
            tmpExp.clear()

        # 分析减法能否满足条件(第i个值和第i+1个值)
        tmpV = dgs[i] - dgs[i + 1]
        tmpVExp = getExp(exp[i], exp[i + 1], "-")
        j = 0
        while j < numLen:
            if j in [i, i + 1]:
                tmpDgs.append(tmpV)
                tmpExp.append(tmpVExp)
                j += 1
            else:
                tmpDgs.append(dgs[j])
                tmpExp.append(f"{exp[j]}")
            j += 1
        if not solve24(tmpDgs, len(tmpDgs), tmpExp):
            tmpDgs.clear()
            tmpExp.clear()

        # 分析乘法能否满足条件(第i个值和第i+1个值)
        tmpV = dgs[i] * dgs[i + 1]
        tmpVExp = getExp(exp[i], exp[i + 1], "*")
        j = 0
        while j < numLen:
            if j in [i, i + 1]:
                tmpDgs.append(tmpV)
                tmpExp.append(tmpVExp)
                j += 1
            else:
                tmpDgs.append(dgs[j])
                tmpExp.append(f"{exp[j]}")
            j += 1
        if not solve24(tmpDgs, len(tmpDgs), tmpExp):
            tmpDgs.clear()
            tmpExp.clear()

        # 分析除法能否满足条件(第i个值和第i+1个值)
        if dgs[i + 1] != 0:
            tmpV = dgs[i] / dgs[i + 1]
            tmpVExp = getExp(exp[i], exp[i + 1], "/")
            j = 0
            while j < numLen:
                if j in [i, i + 1]:
                    tmpDgs.append(tmpV)
                    tmpExp.append(tmpVExp)
                    j += 1
                else:
                    tmpDgs.append(dgs[j])
                    tmpExp.append(f"{exp[j]}")
                j += 1
            if not solve24(tmpDgs, len(tmpDgs), tmpExp):
                tmpDgs.clear()
                tmpExp.clear()

参考结果:

[0] 5*(5-5/5/5)
[1] (5*5*5-5)/5
[2] 5*(5-5/(5*5))
[3] (5-5/5/5)*5
[4] (5-5/(5*5))*5

扩展:给定8个8数字使用 加、减、乘、除,小括号运算使等式等于1000

迭代法实现

def solve1000(dgs: list, numLen: int, exp: list):
    global totalTimes
    totalTimes += 1
    # 验证结果值
    if numLen == 1:
        boFound = abs(dgs[0] - 1000) < epsilon
        if boFound:
            # 用求表达式的值函数验证结果
            real = eval(exp[0])
            print("boFound:", exp[0], dgs, f"real={real} {real==dgs[0]}")
            expSet.add(exp[0])
        return boFound

    # 分析表达式的每一个数字的+-*/是否可以得到目标值,且从第0,1,...个索引值开始两两计算并合并
    for i in range(numLen - 1):
        tmpDgs = []
        tmpExp = []
        # 分析加法能否满足条件(第i个值和第i+1个值)
        tmpV = dgs[i] + dgs[i + 1]
        tmpVExp = getExp(exp[i], exp[i + 1], "+")
        j = 0
        while j < numLen:
            if j in [i, i + 1]:
                tmpDgs.append(tmpV)
                tmpExp.append(tmpVExp)
                j += 1
            else:
                tmpDgs.append(dgs[j])
                tmpExp.append(f"{exp[j]}")
            j += 1
        if not solve1000(tmpDgs, len(tmpDgs), tmpExp):
            tmpDgs.clear()
            tmpExp.clear()

        # 分析减法能否满足条件(第i个值和第i+1个值)
        tmpV = dgs[i] - dgs[i + 1]
        tmpVExp = getExp(exp[i], exp[i + 1], "-")
        j = 0
        while j < numLen:
            if j in [i, i + 1]:
                tmpDgs.append(tmpV)
                tmpExp.append(tmpVExp)
                j += 1
            else:
                tmpDgs.append(dgs[j])
                tmpExp.append(f"{exp[j]}")
            j += 1
        if not solve1000(tmpDgs, len(tmpDgs), tmpExp):
            tmpDgs.clear()
            tmpExp.clear()

        # 分析乘法能否满足条件(第i个值和第i+1个值)
        tmpV = dgs[i] * dgs[i + 1]
        tmpVExp = getExp(exp[i], exp[i + 1], "*")
        j = 0
        while j < numLen:
            if j in [i, i + 1]:
                tmpDgs.append(tmpV)
                tmpExp.append(tmpVExp)
                j += 1
            else:
                tmpDgs.append(dgs[j])
                tmpExp.append(f"{exp[j]}")
            j += 1
        if not solve1000(tmpDgs, len(tmpDgs), tmpExp):
            tmpDgs.clear()
            tmpExp.clear()

        # 分析除法能否满足条件(第i个值和第i+1个值)
        if dgs[i + 1] != 0:
            tmpV = dgs[i] / dgs[i + 1]
            tmpVExp = getExp(exp[i], exp[i + 1], "/")
            j = 0
            while j < numLen:
                if j in [i, i + 1]:
                    tmpDgs.append(tmpV)
                    tmpExp.append(tmpVExp)
                    j += 1
                else:
                    tmpDgs.append(dgs[j])
                    tmpExp.append(f"{exp[j]}")
                j += 1
            if not solve1000(tmpDgs, len(tmpDgs), tmpExp):
                tmpDgs.clear()
                tmpExp.clear()

参考结果:

totalTimes= 93393622
[0] 8+(8-8/(8+8)+8)*8*8
[1] 8*(8*8+8*8)-8-(8+8)
[2] ((8+8)*8-(8+8)/8)*8-8
[3] 8*(8*(8+8)-8/8)-8-8
[4] 8*(8-8/(8+8)+8)*8+8
[5] 8-8*8*(8/(8+8)-8-8)
[6] (8+8)*8*(8-8/8/8)-8
[7] 8-((8+8)/8-8*8)*(8+8)
[8] (8*(8+8)-8/8)*8-8-8
[9] 8-(8+8)*((8+8)/8-8*8)
[10] (8+8)*(8-8/8/8)*8-8
[11] 8-8*8*(8/(8+8)-(8+8))
[12] 8*(8*(8+8)-(8+8)/8)-8
[13] (8-8/(8*8))*8*(8+8)-8
[14] (8*8+8*8)*8-8-8-8
[15] (8*(8+8)-8/8)*8-(8+8)
[16] (8+8)*8*(8-8/(8*8))-8
[17] 8*8*(8-8/(8+8)+8)+8
[18] (8+8)/8*(8*8*8-8)-8
[19] 8+(8*8-(8+8)/8)*(8+8)
[20] (8-(8/(8+8)-8))*8*8+8
[21] (8*8*8-8)/8*(8+8)-8
[22] (8*8+8*8)*8-(8+8+8)
[23] 8*8*(8-(8/(8+8)-8))+8
[24] 8+8*(8-8/(8+8)+8)*8
[25] 8*8*(8+8-8/(8+8))+8
[26] (8*(8+8)-(8+8+8)/8)*8
[27] (8*8+8*8)*8-8-(8+8)
[28] 8+(8+8-8/(8+8))*8*8
[29] (8-8/8/8)*(8+8)*8-8
[30] 8*(8*8+8*8)-(8+8+8)
[31] 8*(8*(8+8)-8/8)-(8+8)
[32] 8+8*(8+8-8/(8+8))*8
[33] 8-8*(8/(8+8)-(8+8))*8
[34] 8*((8+8)*8-8/8)-8-8
[35] (8+8)*(8*8*8-8)/8-8
[36] ((8+8)*8-8/8)*8-8-8
[37] (8+8)*(8*8-(8+8)/8)+8
[38] 8*(8+8)*(8-8/8/8)-8
[39] 8-(8/(8+8)-8-8)*8*8
[40] ((8+8)*8-8/8)*8-(8+8)
[41] (8*8+8*8)*8-(8+8)-8
[42] 8*(8+8)*(8-8/(8*8))-8
[43] 8+8*8*(8-8/(8+8)+8)
[44] 8-8*(8/(8+8)-8-8)*8
[45] 8*((8+8)*8-(8+8)/8)-8
[46] (8*8*8-8)/(8/(8+8))-8
[47] 8*((8+8)*8-(8+8+8)/8)
[48] (8-8/(8+8)+8)*8*8+8
[49] 8-(8/(8+8)-(8+8))*8*8
[50] (8+8-8/(8+8))*8*8+8
[51] 8+8*8*(8+8-8/(8+8))
[52] (8-8/8/8)*8*(8+8)-8
[53] (8*(8+8)-(8+8)/8)*8-8
[54] 8*(8*(8+8)-(8+8+8)/8)
[55] 8*(8-(8/(8+8)-8))*8+8
[56] 8*(8-8/8/8)*(8+8)-8
[57] (8+8)/(8/(8*8*8-8))-8
[58] (8*8*8-8)*(8+8)/8-8
[59] 8+8*8*(8-(8/(8+8)-8))
[60] 8+(8+8)*(8*8-(8+8)/8)
[61] 8*(8+8-8/(8+8))*8+8
[62] ((8+8)*8-(8+8+8)/8)*8
[63] 8*(8-8/(8*8))*(8+8)-8
[64] 8*(8*8+8*8)-8-8-8
[65] 8*(8*8+8*8)-(8+8)-8
[66] 8+8*(8-(8/(8+8)-8))*8
[67] 8+(8-(8/(8+8)-8))*8*8
[68] (8*8-(8+8)/8)*(8+8)+8
[69] (8-8/(8*8))*(8+8)*8-8
[70] (8+8)*(8-8/(8*8))*8-8
[71] 8*((8+8)*8-8/8)-(8+8)

全部代码:

"""
21,24点问题:
1.全排列组合和笛卡尔积实现
2.迭代法实现
注:当前算法并没有将问题的所有解全部列出来
  求所有解参考方案:需要对每个不同的数字进行掉换然后重新再计算(问题复杂度上升几个数量级)
"""
from itertools import permutations, product

# 记录运行的总次数
totalTimes: int = 0
# 结果集(除去重复的表达式)
expSet = set()
# 浮点数相等比较误差值
epsilon = 0.0000001

def solve_exp21(nums):
    if len(nums) != 4:
        print("Error:数字个数必须为4")
        return
    global totalTimes
    # 生成所有可能的排列和运算符组合
    for perm in permutations(nums):
        # print(perm)
        # 笛卡尔积(repeat=3?数字中间最多只有3个符号,可重复使用)
        for ops in product('+-*/', repeat=3):
            # d:digit o:ops
            fmt0 = "d" * 7
            fmt1 = "(dod)odod"
            fmt10 = "((dod)od)od"
            fmt11 = "(dod)o(dod)"
            fmt2 = "do(dod)od"
            fmt20 = "(do(dod))od"
            fmt21 = "do((dod)od)"
            fmt3 = "dodo(dod)"
            fmt30 = "(dod)o(dod)"
            fmt31 = "do(do(dod))"
            fmtlst = [fmt0, fmt1, fmt10, fmt11, fmt2, fmt20, fmt21, fmt3, fmt30, fmt31]
            for tmpFmt in fmtlst:
                totalTimes += 1
                tmpFmt = tmpFmt.replace("d","{}")
                tmpFmt = tmpFmt.replace("o","{}")
                tmpExp = tmpFmt.format(perm[0], ops[0], perm[1], ops[1], perm[2], ops[2], perm[3])
                value = eval(tmpExp)
                if abs(value - 21) < epsilon:
                    expSet.add(tmpExp)

# 将表达式内的括号运算变成一个数字符号(用于是否需要加小上括号运算)
def squeezeExp(exp: str):
    while True:
        eIdx = exp.find(")")
        sIdx = exp[0:eIdx].rfind("(")
        if eIdx == -1:
            break
        exp = exp[0:sIdx] + "x" + exp[eIdx + 1:]
    return exp

# 统计运算符数量(用于是否需要加上括号运算)
def histSymbols(exp):
    CON_SYMS = ["+", "-", "*", "/"]
    rlts = [0] * len(CON_SYMS)
    for index in range(len(CON_SYMS)):
        tmpSym = CON_SYMS[index]
        tmpCnt = 0
        for tmpC in exp:
            if tmpC == tmpSym:
                tmpCnt += 1
        rlts[index] = tmpCnt
    totalSys = 0
    for tmpCnt in rlts:
        totalSys += tmpCnt
    return rlts, totalSys

# 生成计算表达式(在支持四则运算优先级的情况下,减少冗余括号)
def getExp(exp1: str, exp2: str, opTag: str):
    if opTag == "+":
        return f"{exp1}+{exp2}"
    elif opTag == "-":
        squExpA = squeezeExp(exp1)
        squExpB = squeezeExp(exp2)
        expA = exp1
        expB = exp2
        if len(squExpB) > 1:
            # 全*/号不需要加括号
            symsHist, total = histSymbols(squExpB)
            if symsHist[2] + symsHist[3] != total:
                expB = "(" + expB + ")"
        return f"{expA}-{expB}"
    elif opTag == "*":
        squExpA = squeezeExp(exp1)
        squExpB = squeezeExp(exp2)
        expA = exp1
        expB = exp2
        if len(squExpA) > 1:
            symsHist, total = histSymbols(squExpA)
            if symsHist[2] + symsHist[3] != total:
                expA = "(" + expA + ")"
        if len(squExpB) > 1:
            symsHist, total = histSymbols(squExpB)
            if symsHist[2] + symsHist[3] != total:
                expB = "(" + expB + ")"
        return f"{expA}*{expB}"
    elif opTag == "/":
        squExpA = squeezeExp(exp1)
        squExpB = squeezeExp(exp2)
        expA = exp1
        expB = exp2
        if len(squExpA) > 1:
            symsHist, total = histSymbols(squExpA)
            if symsHist[2] + symsHist[3] != total:
                expA = "(" + expA + ")"
        if len(squExpB) > 1:
                expB = "(" + expB + ")"
        return f"{expA}/{expB}"


def solve24(dgs: list, numLen: int, exp: list):
    global totalTimes
    totalTimes += 1
    # 验证结果值
    if numLen == 1:
        boFound = abs(dgs[0] - 24) < epsilon
        if boFound:
            # 用求表达式的值函数验证结果
            real = eval(exp[0])
            print("boFound:", exp[0], dgs, f"real={real} {real==dgs[0]}")
            expSet.add(exp[0])
        return boFound

    # 分析表达式的每一个数字的+-*/是否可以得到目标值,且从第0,1,...个索引值开始两两计算并合并
    for i in range(numLen - 1):
        tmpDgs = []
        tmpExp = []
        # 分析加法能否满足条件(第i个值和第i+1个值)
        tmpV = dgs[i] + dgs[i + 1]
        tmpVExp = getExp(exp[i], exp[i + 1], "+")
        j = 0
        while j < numLen:
            if j in [i, i + 1]:
                tmpDgs.append(tmpV)
                tmpExp.append(tmpVExp)
                j += 1
            else:
                tmpDgs.append(dgs[j])
                tmpExp.append(f"{exp[j]}")
            j += 1
        if not solve24(tmpDgs, len(tmpDgs), tmpExp):
            tmpDgs.clear()
            tmpExp.clear()

        # 分析减法能否满足条件(第i个值和第i+1个值)
        tmpV = dgs[i] - dgs[i + 1]
        tmpVExp = getExp(exp[i], exp[i + 1], "-")
        j = 0
        while j < numLen:
            if j in [i, i + 1]:
                tmpDgs.append(tmpV)
                tmpExp.append(tmpVExp)
                j += 1
            else:
                tmpDgs.append(dgs[j])
                tmpExp.append(f"{exp[j]}")
            j += 1
        if not solve24(tmpDgs, len(tmpDgs), tmpExp):
            tmpDgs.clear()
            tmpExp.clear()

        # 分析乘法能否满足条件(第i个值和第i+1个值)
        tmpV = dgs[i] * dgs[i + 1]
        tmpVExp = getExp(exp[i], exp[i + 1], "*")
        j = 0
        while j < numLen:
            if j in [i, i + 1]:
                tmpDgs.append(tmpV)
                tmpExp.append(tmpVExp)
                j += 1
            else:
                tmpDgs.append(dgs[j])
                tmpExp.append(f"{exp[j]}")
            j += 1
        if not solve24(tmpDgs, len(tmpDgs), tmpExp):
            tmpDgs.clear()
            tmpExp.clear()

        # 分析除法能否满足条件(第i个值和第i+1个值)
        if dgs[i + 1] != 0:
            tmpV = dgs[i] / dgs[i + 1]
            tmpVExp = getExp(exp[i], exp[i + 1], "/")
            j = 0
            while j < numLen:
                if j in [i, i + 1]:
                    tmpDgs.append(tmpV)
                    tmpExp.append(tmpVExp)
                    j += 1
                else:
                    tmpDgs.append(dgs[j])
                    tmpExp.append(f"{exp[j]}")
                j += 1
            if not solve24(tmpDgs, len(tmpDgs), tmpExp):
                tmpDgs.clear()
                tmpExp.clear()

def solve1000(dgs: list, numLen: int, exp: list):
    global totalTimes
    totalTimes += 1
    # 验证结果值
    if numLen == 1:
        boFound = abs(dgs[0] - 1000) < epsilon
        if boFound:
            # 用求表达式的值函数验证结果
            real = eval(exp[0])
            print("boFound:", exp[0], dgs, f"real={real} {real==dgs[0]}")
            expSet.add(exp[0])
        return boFound

    # 分析表达式的每一个数字的+-*/是否可以得到目标值,且从第0,1,...个索引值开始两两计算并合并
    for i in range(numLen - 1):
        tmpDgs = []
        tmpExp = []
        # 分析加法能否满足条件(第i个值和第i+1个值)
        tmpV = dgs[i] + dgs[i + 1]
        tmpVExp = getExp(exp[i], exp[i + 1], "+")
        j = 0
        while j < numLen:
            if j in [i, i + 1]:
                tmpDgs.append(tmpV)
                tmpExp.append(tmpVExp)
                j += 1
            else:
                tmpDgs.append(dgs[j])
                tmpExp.append(f"{exp[j]}")
            j += 1
        if not solve1000(tmpDgs, len(tmpDgs), tmpExp):
            tmpDgs.clear()
            tmpExp.clear()

        # 分析减法能否满足条件(第i个值和第i+1个值)
        tmpV = dgs[i] - dgs[i + 1]
        tmpVExp = getExp(exp[i], exp[i + 1], "-")
        j = 0
        while j < numLen:
            if j in [i, i + 1]:
                tmpDgs.append(tmpV)
                tmpExp.append(tmpVExp)
                j += 1
            else:
                tmpDgs.append(dgs[j])
                tmpExp.append(f"{exp[j]}")
            j += 1
        if not solve1000(tmpDgs, len(tmpDgs), tmpExp):
            tmpDgs.clear()
            tmpExp.clear()

        # 分析乘法能否满足条件(第i个值和第i+1个值)
        tmpV = dgs[i] * dgs[i + 1]
        tmpVExp = getExp(exp[i], exp[i + 1], "*")
        j = 0
        while j < numLen:
            if j in [i, i + 1]:
                tmpDgs.append(tmpV)
                tmpExp.append(tmpVExp)
                j += 1
            else:
                tmpDgs.append(dgs[j])
                tmpExp.append(f"{exp[j]}")
            j += 1
        if not solve1000(tmpDgs, len(tmpDgs), tmpExp):
            tmpDgs.clear()
            tmpExp.clear()

        # 分析除法能否满足条件(第i个值和第i+1个值)
        if dgs[i + 1] != 0:
            tmpV = dgs[i] / dgs[i + 1]
            tmpVExp = getExp(exp[i], exp[i + 1], "/")
            j = 0
            while j < numLen:
                if j in [i, i + 1]:
                    tmpDgs.append(tmpV)
                    tmpExp.append(tmpVExp)
                    j += 1
                else:
                    tmpDgs.append(dgs[j])
                    tmpExp.append(f"{exp[j]}")
                j += 1
            if not solve1000(tmpDgs, len(tmpDgs), tmpExp):
                tmpDgs.clear()
                tmpExp.clear()

def solve24_test():
    global totalTimes
    totalTimes = 0
    expSet.clear()
    dgs = [5] * 5
    numLen = len(dgs)
    exp = ["5"] * 5
    solve24(dgs, numLen, exp)
    print("totalTimes=", totalTimes)
    index = 0
    for tmpExp in expSet:
        print("[%d] %s" % (index, tmpExp))
        index += 1

def solve1000_test():
    global totalTimes
    totalTimes = 0
    expSet.clear()
    dgs = [8] * 8
    numLen = len(dgs)
    exp = ["8"] * 8
    solve1000(dgs, numLen, exp)
    print("totalTimes=", totalTimes)
    index = 0
    for tmpExp in expSet:
        print("[%d] %s" % (index, tmpExp))
        index += 1

def solve_exp_test():
    global totalTimes
    totalTimes = 0
    expSet.clear()
    # 测试
    nums = [4, 5, 6, 7]
    solve_exp21(nums)
    print("totalTimes=", totalTimes)
    index = 0
    for tmpExp in expSet:
        print("[%d] %s" % (index, tmpExp))
        index += 1

if __name__ == "__main__":
    # solve24_test()
    # solve1000_test()
    solve_exp_test()
  • 24
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值