# Python7-25 用扑克牌计算24点 (25 分)

2 3 12 12

((3-2)12)+12

a,b,c,d=map(int,input().split())
list1 = [a, b, c, d]
list2 = []
list3 = []
symbols = ["+", "-", "
", "/"]
class FindException(Exception):
pass
try:
for i in range(4):
one = list1[i]
list2 = list1[0:i] + list1[i + 1:]
for j in range(3):
two = list2[j]
list3 = list2[0:j] + list2[j + 1:]
for k in range(2):
three = list3[k]
four = (list3[0:k] + list3[k + 1:])[0]
for s1 in symbols:
for s2 in symbols:
for s3 in symbols:
express = "((one{0}two){1}three){2}four".format(s1, s2, s3)
if eval(express) == 24:
print("(({0}{1}{2}){3}{4}){5}{6} ".format(one, s1, two, s2, three, s3, four))
raise FindException
print("-1")
except FindException:
pass

• 写回答

#### 3条回答默认 最新

• 雷昂纳多·梅西 2021-07-02 13:02
关注

a,b,c,d=map(int,input().split())
list1 = [a, b, c, d]
list2 = []
list3 = []
symbols = ["+", "-", "*", "/"]
class FindException(Exception):
pass
try:
for i in range(4):
one = list1[i]
list2 = list1[0:i] + list1[i + 1:]
for j in range(3):
two = list2[j]
list3 = list2[0:j] + list2[j + 1:]
for k in range(2):
three = list3[k]
four = (list3[0:k] + list3[k + 1:])[0]
for s1 in symbols:
for s2 in symbols:
for s3 in symbols:
express = "((one{0}two){1}three){2}four".format(s1, s2, s3)
if eval(express) == 24:
print("(({0}{1}{2}){3}{4}){5}{6} ".format(one, s1, two, s2, three, s3, four))
raise FindException
print("-1")
except FindException:
pass

本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

#### 悬赏问题

• ¥50 matlab，文献复现