首先我想到应该枚举每一种情况,但是怎么去枚举是一个很大的问题
观察计算过程,我发现算24点的本质其实就是每次计算两个数,得出一个新的数,然后再进行运算
例如 计算 3*(3- 3/8) = 24时
先计算3/8
然后我们可以将问题转化为 3,3,3/8三个数是否能得到24
以此类推
于是,我想到可以从4个数选出2个数,对他们进行不同的运算,然后将得到的结果作为新数,然后将这3个数中选出2个数继续这样做,直到结果等于24
我们可以将问题推广到n的情况:
如果给定n个数,以及m,问能否对他们进行四则运算,使得结果等于m?
先选定两个数,进行运算后得到新数,然后判断剩下的n-1个数能否得到m
很自然能想到使用递归去做
但是,我们能否让程序输出怎么算的呢?
当然可以!
我定义了一个Num类,它有两个属性,value和formula
1 # 储存数字的数值和表达式的类 2 class Num: 3 def __init__(self, value): 4 self.value = float(value) 5 self.formula = str(value)
value表示这个数字的值
formula则储存了这个数字的表达式,初始情况下,formula=str(value)
函数calculate(length, list1, ans)用于计算在listh1列表中的length个数能否得到ans
首先当length == 1时,直接看那一个是否等于ans:(因为有除法运算,所以用float)
1 if length == 1: 2 if abs(list1[0].value - ans) <= 1e-9: 3 print("%s = %d" % (list1[0].formula, ans)) 4 return True 5 return False
如果lengrh>1,取list1里的两个数,枚举所有情况,这里以除法为例
1 if abs(a) >= 1e-9: 2 temp = list1[:] 3 c = Num(b / a) 4 c.formula = "%s / %s" % (list1[j].formula, list1[i].formula) 5 temp.append(c) 6 del temp[j] 7 del temp[i] 8 if calculate(len(temp), temp, ans): 9 return True
temp是新的列表,里面删去了我正在枚举的数a,b,并加入了b / a
第四行表示 新数的表达式 = a的表达式 + '/' + b的表达式
举个例子
三个数 2 7 5
(2 + 7) * 5 = 49
一开始列表为
value | formula | |
list1[0] | 2 | 2 |
list1[1] | 7 | 7 |
list1[2] | 5 | 5 |
枚举到2+7时
列表变为
value | formula | |
list1[0] | 5 | (2+7) |
list1[1] | 9 | 9 |
继续
value | formula | |
list1[0] | 45 | (2+7)*9 |
上代码!
1 # 储存数字的数值和表达式的类 2 class Num: 3 def __init__(self, value): 4 self.value = float(value) 5 self.formula = str(value) 6 7 8 # 读入所有数,并转化为Num对象 9 num_list = list(map(Num, input().split())) 10 answer = int(input()) 11 12 13 # 函数主体 14 def calculate(length, list1, ans): 15 # 如果计算结束了 16 if length == 1: 17 # 如果答案和ans相同,输出计算过程 18 if abs(list1[0].value - ans) <= 1e-9: 19 print("%s = %d" % (list1[0].formula, ans)) 20 return True 21 return False 22 23 # 选两个数出来 24 for i in range(length): 25 for j in range(i + 1, length): 26 27 # 为方便,用a,b来储存值 28 a = list1[i].value 29 b = list1[j].value 30 31 # 接下来枚举所有的四则运算,共6种 32 # a+b a-b b-a a*b a/b b/a 33 34 # a+b 35 temp = list1[:] 36 c = Num(a + b) 37 c.formula = "(%s + %s)" % (list1[i].formula, list1[j].formula) 38 temp.append(c) 39 del temp[j] 40 del temp[i] 41 # 递归计算 42 if calculate(len(temp), temp, ans): 43 return True 44 45 # a-b 46 temp = list1[:] 47 c = Num(a - b) 48 c.formula = "(%s - %s)" % (list1[i].formula, list1[j].formula) 49 temp.append(c) 50 del temp[j] 51 del temp[i] 52 53 if calculate(len(temp), temp, ans): 54 return True 55 56 # b-a 57 temp = list1[:] 58 c = Num(b - a) 59 c.formula = "(%s - %s)" % (list1[j].formula, list1[i].formula) 60 temp.append(c) 61 del temp[j] 62 del temp[i] 63 if calculate(len(temp), temp, ans): 64 return True 65 66 # a*b 67 temp = list1[:] 68 c = Num(a * b) 69 c.formula = "%s * %s" % (list1[i].formula, list1[j].formula) 70 temp.append(c) 71 del temp[j] 72 del temp[i] 73 if calculate(len(temp), temp, ans): 74 return True 75 76 # a/b 77 # 如果b不为零 78 if abs(b) >= 1e-9: 79 temp = list1[:] 80 c = Num(a / b) 81 c.formula = "%s / %s" % (list1[i].formula, list1[j].formula) 82 temp.append(c) 83 del temp[j] 84 del temp[i] 85 if calculate(len(temp), temp, ans): 86 return True 87 88 # b/a 89 if abs(a) >= 1e-9: 90 temp = list1[:] 91 c = Num(b / a) 92 c.formula = "%s / %s" % (list1[j].formula, list1[i].formula) 93 temp.append(c) 94 del temp[j] 95 del temp[i] 96 if calculate(len(temp), temp, ans): 97 return True 98 # 枚举结束 99 return False 100 101 102 if not calculate(len(num_list), num_list, answer): 103 print("False")