题解 | 穷举#24点运算#
24点运算
http://www.nowcoder.com/practice/7e124483271e4c979a82eb2956544f9d
import itertools
def change_str(str_cal):
str_cal = str_cal.replace('11','J').replace('12','Q').replace('13','K').replace('1','A')
return str_cal
def check_str():
num =[]
cal = ['+','-','*','/']
for t in s:
if t == 'JOKER' or t == 'joker':
return "ERROR"
elif t in lst1:
num.append(lst2[lst1.index(t)])
else:
num.append(t)
cal_comb = list(itertools.product(cal,repeat=3))
num_comb = list(itertools.permutations(num,4))
cn_comb = []
for m in num_comb:
for n in cal_comb:
new_cal = m[0]
str_cal = m[0]
for i in range(3):
new_cal = str(int(eval(new_cal+n[i]+m[i+1])))
str_cal += n[i] + m[i+1]
if new_cal == '24':
return change_str(str_cal)
else:
return "NONE"
while True:
try:
s= input().split(" ")
lst1 = "JQKA"
lst2 = ['11','12','13','1']
print(check_str())
except:
break