首先我想到應該枚舉每一種情況,但是怎么去枚舉是一個很大的問題
觀察計算過程,我發現算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")