暴力穷举所有可能。

对输入数据使用排列共有24中可能

permutations(pokes)

对于运算符号则可以重复排列,共有64种可能:

calc = dict(zip(['+', '-', '*', '/'], [lambda x, y: x+y,lambda x, y: x-y,lambda x, y: x*y,lambda x, y: x//y]))
product(calc, repeat=3)

总的循环次数24*64=1536种,然后使用一个双层循环即可。

代码如下:

from itertools import permutations, product

db = dict(zip('3 4 5 6 7 8 9 10 J Q K A 2'.split(), [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2]))
calc = dict(zip(['+', '-', '*', '/'], [lambda x, y: x+y,lambda x, y: x-y,lambda x, y: x*y,lambda x, y: x//y]))

def func(pokes):
    if 'joker' in pokes or '' in pokes:
        return 'ERROR'
    for a1, a2, a3, a4 in permutations(pokes):
        for c1, c2, c3 in product(calc, repeat=3):
            # print(a1, a2, a3, a4, c1, c2,c3)
            if calc[c3](calc[c2](calc[c1](db[a1], db[a2]), db[a3]), db[a4]) == 24:
                return f'{a1}{c1}{a2}{c2}{a3}{c3}{a4}'
    return 'NONE'

while True:
    try:
        # n = input()
        print(func(input().split()))
    except EOFError:
        break