题解 | #密码截取#
24点运算
http://www.nowcoder.com/practice/7e124483271e4c979a82eb2956544f9d
import itertools
def jjj(i,jj):
for j in jj:
re = ip_dict2[i[0]] + j[0] + ip_dict2[i[1]] + j[1] + ip_dict2[i[2]] + j[2] + ip_dict2[i[3]]
re2 = "(" + "(" + str(i[0]) + j[0] + str(i[1]) + ")" + j[1] + str(i[2]) + ")" + j[2] + str(i[3])
re = re.replace("//", "/")
all_result.append((i))
if eval(re2) == 24:
out.append(re)
return out
return False
def dfx(iplist, ysflist):
ii = list(itertools.permutations(iplist, 4))
jj = list(itertools.product(ysflist, repeat = 3))
all_result = []
for i in ii:
if jjj(i, jj):
return True
return False
while True:
try:
ysf_list, ip_list, ip_dict, ip_dict2 =["+", "-", "*", "//"], [], {"J":11, "Q":12, "K":13, "A":1}, {11:"J", 12:"Q", 13:"K", 1:"A", 2:"2", 3:"3", 4:"4", 5:"5", 6:"6", 7:"7", 8:"8", 9:"9", 10:"10"}
for i in input().split(" "):
if i in ("2","3","4","5","6","7","8","9","10"):
ip_list.append(int(i))
else:
if i in ("joker", "JOKER"):
ip_list = []
break
else:
ip_list.append(ip_dict[i])
if ip_list:
out = []
all_result = []
if dfx(ip_list, ysf_list):
print(out[0])
else:
print("NONE")
else:
print("ERROR")
except Exception as e:
print(e)
break