题解 | #24点运算#
24点运算
https://www.nowcoder.com/practice/7e124483271e4c979a82eb2956544f9d
from itertools import permutations, product
cards=['','A']+list(map(str,range(2, 11)))+['J','Q','K']
a=input().split()
opts=['+','-','*','/']
def calc(a,b,opt):
if opt=='+':
return a+b
elif opt=='-':
return a-b
elif opt=='*':
return a*b
else:
return a//b
def test(a):
for i in permutations(a): # 不可重复选取的所有排列
ret=''
for j in product(opts,repeat=3): # 可重复选取的所有排列
res=calc(calc(calc(i[0], i[1], j[0]), i[2], j[1]), i[3],j[2])
if res==24:
for k in range(3):
ret+=cards[i[k]]+j[k]
ret+=cards[i[3]]
return ret
return 'NONE'
if 'joker' in a or 'JOKER' in a:
print('ERROR')
else:
a=[cards.index(i) for i in a]
if a[0]*a[1]*a[2]*a[3]<24:
print('NONE')
else:
print(test(a))
