题解 | #24点运算#
24点运算
https://www.nowcoder.com/practice/7e124483271e4c979a82eb2956544f9d
a = input().split() c = {'3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, 'J': 11, 'Q': 12, 'K': 13, 'A': 1, '2': 2} d = dict((str(y), x) for x,y in c.items()) def b(a, num, s): if len(a) == 0: if num == 24: print(''.join(d.get(x, x) for x in s)) return True else: return False for i, x in enumerate(a): a1 = a[0:i]+a[i+1:] if b(a1, num+x, s+['+',str(x)]): return True if b(a1, num-x, s+['-',str(x)]): return True if b(a1, num*x, s+['*',str(x)]): return True if b(a1, num//x, s+['/',str(x)]): return True return False for i, x in enumerate(a): if x not in c: print('ERROR') break else: a[i] = c[x] else: for i, x in enumerate(a): a1 = a[0:i]+a[i+1:] if b(a1, x, [str(x)]): break else: print('NONE')