小浩演算法|一文讓你學會如何用程式碼判斷"24"點
“24點”是一種數學遊戲,正如象棋、圍棋一樣是一種人們喜聞樂見的娛樂活動。它始於何年何月已無從考究,但它以自己獨具的數學魅力和豐富的內涵正逐漸被越來越多的人們所接受。今天就為大家分享一道關於“24點”的演算法題目。
話不多說,直接看題。
題目:你有 4 張寫有 1 到 9 數字的牌。你需要判斷是否能通過 *,/,+,-,(,) 的運算得到 24。
示例 1:
輸入: [4, 1, 8, 7]
輸出: True
解釋: (8-4) * (7-1) = 24
示例 2:
輸入: [1, 2, 1, 2]
輸出: False
注意:
- 除法運算子 / 表示實數除法,而不是整數除法。例如 :4 / (1 - 2/3) = 12 。
- 每個運算子對兩個數進行運算。特別是我們不能用 - 作為一元運算子。例如,[1, 1, 1, 1] 作為輸入時,表示式 -1 - 1 - 1 - 1 是不允許的。
- 你不能將數字連線在一起。例如,輸入為 [1, 2, 1, 2] 時,不能寫成 12 + 12 。
題目分析
拿到題目,第一反應就可以想到暴力求解。如果我們要判斷給出的4張牌是否可以通過組合得到24,那我們只需找出所有的可組合的方式進行遍歷。
4個數字,3個操作符,外加括號,基本目測就能想到組合數不會大到超出邊界。所以,我們只要把他們統統列出來,不就可以進行求解了嗎?說幹就幹!
我們首先定義個方法,用來判斷兩個數的的所有操作符組合是否可以得到24。
func judgePoint24_2(a, b float64) bool { return a+b == 24 || a*b == 24 || a-b == 24 || b-a == 24 || a/b == 24 || b/a == 24 }
但是這個方法寫的正確嗎?其實不對!因為在計算機中,實數在計算和儲存過程中會有一些微小的誤差,對於一些與零作比較的語句來說,有時會因誤差而導致原本是等於零但結果卻小於或大於零之類的情況發生,所以常用一個很小的數 1e-6 代替 0,進行判讀!
(1e-6:表示1乘以10的負6次方。Math.abs(x)<1e-6 其實相當於x==0。1e-6(也就是0.000001)叫做epslon,用來抵消浮點運算中因為誤差造成的相等無法判斷的情況。這個知識點需要掌握!)
舉個例子:
func main() { var a float64 var b float64 b = 2.0 //math.Sqrt:開平方根 c := math.Sqrt(2) a = b - c*c fmt.Println(a == 0) //false fmt.Println(a < 1e-6 && a > -(1e-6)) //true }
這裡直接用 a==0 就會得到false,但是通過 a < 1e-6 && a > -(1e-6) 卻可以進行準確的判斷。
所以我們將上面的方法改寫:
//go語言 //judgePoint24_2:判斷兩個數的所有操作符組合是否可以得到24 func judgePoint24_2(a, b float64) bool { return (a+b < 24+1e-6 && a+b > 24-1e-6) || (a*b < 24+1e-6 && a*b > 24-1e-6) || (a-b < 24+1e-6 && a-b > 24-1e-6) || (b-a < 24+1e-6 && b-a > 24-1e-6) || (a/b < 24+1e-6 && a/b > 24-1e-6) || (b/a < 24+1e-6 && b/a > 24-1e-6) }
完善了通過兩個數來判斷是否可以得到24的方法,現在我們加一個判斷三個數是否可以得到24的方法。
//硬核程式碼,不服來辯! func judgePoint24_3(a, b, c float64) bool { return judgePoint24_2(a+b, c) || judgePoint24_2(a-b, c) || judgePoint24_2(a*b, c) || judgePoint24_2(a/b, c) || judgePoint24_2(b-a, c) || judgePoint24_2(b/a, c) || judgePoint24_2(a+c, b) || judgePoint24_2(a-c, b) || judgePoint24_2(a*c, b) || judgePoint24_2(a/c, b) || judgePoint24_2(c-a, b) || judgePoint24_2(c/a, b) || judgePoint24_2(c+b, a) || judgePoint24_2(c-b, a) || judgePoint24_2(c*b, a) || judgePoint24_2(c/b, a) || judgePoint24_2(b-c, a) || judgePoint24_2(b/c, a) }
好了。三個數的也出來了,我們再加一個判斷4個數為24點的方法:(排列組合,我想大家都會....)
前方高能!!!
前方高能!!!
前方高能!!!
//硬核程式碼,不服來辯! func judgePoint24(nums []int) bool { return judgePoint24_3(float64(nums[0])+float64(nums[1]), float64(nums[2]), float64(nums[3])) || judgePoint24_3(float64(nums[0])-float64(nums[1]), float64(nums[2]), float64(nums[3])) || judgePoint24_3(float64(nums[0])*float64(nums[1]), float64(nums[2]), float64(nums[3])) || judgePoint24_3(float64(nums[0])/float64(nums[1]), float64(nums[2]), float64(nums[3])) || judgePoint24_3(float64(nums[1])-float64(nums[0]), float64(nums[2]), float64(nums[3])) || judgePoint24_3(float64(nums[1])/float64(nums[0]), float64(nums[2]), float64(nums[3])) || judgePoint24_3(float64(nums[0])+float64(nums[2]), float64(nums[1]), float64(nums[3])) || judgePoint24_3(float64(nums[0])-float64(nums[2]), float64(nums[1]), float64(nums[3])) || judgePoint24_3(float64(nums[0])*float64(nums[2]), float64(nums[1]), float64(nums[3])) || judgePoint24_3(float64(nums[0])/float64(nums[2]), float64(nums[1]), float64(nums[3])) || judgePoint24_3(float64(nums[2])-float64(nums[0]), float64(nums[1]), float64(nums[3])) || judgePoint24_3(float64(nums[2])/float64(nums[0]), float64(nums[1]), float64(nums[3])) || judgePoint24_3(float64(nums[0])+float64(nums[3]), float64(nums[2]), float64(nums[1])) || judgePoint24_3(float64(nums[0])-float64(nums[3]), float64(nums[2]), float64(nums[1])) || judgePoint24_3(float64(nums[0])*float64(nums[3]), float64(nums[2]), float64(nums[1])) || judgePoint24_3(float64(nums[0])/float64(nums[3]), float64(nums[2]), float64(nums[1])) || judgePoint24_3(float64(nums[3])-float64(nums[0]), float64(nums[2]), float64(nums[1])) || judgePoint24_3(float64(nums[3])/float64(nums[0]), float64(nums[2]), float64(nums[1])) || judgePoint24_3(float64(nums[2])+float64(nums[3]), float64(nums[0]), float64(nums[1])) || judgePoint24_3(float64(nums[2])-float64(nums[3]), float64(nums[0]), float64(nums[1])) || judgePoint24_3(float64(nums[2])*float64(nums[3]), float64(nums[0]), float64(nums[1])) || judgePoint24_3(float64(nums[2])/float64(nums[3]), float64(nums[0]), float64(nums[1])) || judgePoint24_3(float64(nums[3])-float64(nums[2]), float64(nums[0]), float64(nums[1])) || judgePoint24_3(float64(nums[3])/float64(nums[2]), float64(nums[0]), float64(nums[1])) || judgePoint24_3(float64(nums[1])+float64(nums[2]), float64(nums[0]), float64(nums[3])) || judgePoint24_3(float64(nums[1])-float64(nums[2]), float64(nums[0]), float64(nums[3])) || judgePoint24_3(float64(nums[1])*float64(nums[2]), float64(nums[0]), float64(nums[3])) || judgePoint24_3(float64(nums[1])/float64(nums[2]), float64(nums[0]), float64(nums[3])) || judgePoint24_3(float64(nums[2])-float64(nums[1]), float64(nums[0]), float64(nums[3])) || judgePoint24_3(float64(nums[2])/float64(nums[1]), float64(nums[0]), float64(nums[3])) || judgePoint24_3(float64(nums[1])+float64(nums[3]), float64(nums[2]), float64(nums[0])) || judgePoint24_3(float64(nums[1])-float64(nums[3]), float64(nums[2]), float64(nums[0])) || judgePoint24_3(float64(nums[1])*float64(nums[3]), float64(nums[2]), float64(nums[0])) || judgePoint24_3(float64(nums[1])/float64(nums[3]), float64(nums[2]), float64(nums[0])) || judgePoint24_3(float64(nums[3])-float64(nums[1]), float64(nums[2]), float64(nums[0])) || judgePoint24_3(float64(nums[3])/float64(nums[1]), float64(nums[2]), float64(nums[0])) }
Go語言示例
搞定收工,我們整合全部程式碼如下:
//硬核程式設計... func judgePoint24(nums []int) bool { return judgePoint24_3(float64(nums[0])+float64(nums[1]), float64(nums[2]), float64(nums[3])) || judgePoint24_3(float64(nums[0])-float64(nums[1]), float64(nums[2]), float64(nums[3])) || judgePoint24_3(float64(nums[0])*float64(nums[1]), float64(nums[2]), float64(nums[3])) || judgePoint24_3(float64(nums[0])/float64(nums[1]), float64(nums[2]), float64(nums[3])) || judgePoint24_3(float64(nums[1])-float64(nums[0]), float64(nums[2]), float64(nums[3])) || judgePoint24_3(float64(nums[1])/float64(nums[0]), float64(nums[2]), float64(nums[3])) || judgePoint24_3(float64(nums[0])+float64(nums[2]), float64(nums[1]), float64(nums[3])) || judgePoint24_3(float64(nums[0])-float64(nums[2]), float64(nums[1]), float64(nums[3])) || judgePoint24_3(float64(nums[0])*float64(nums[2]), float64(nums[1]), float64(nums[3])) || judgePoint24_3(float64(nums[0])/float64(nums[2]), float64(nums[1]), float64(nums[3])) || judgePoint24_3(float64(nums[2])-float64(nums[0]), float64(nums[1]), float64(nums[3])) || judgePoint24_3(float64(nums[2])/float64(nums[0]), float64(nums[1]), float64(nums[3])) || judgePoint24_3(float64(nums[0])+float64(nums[3]), float64(nums[2]), float64(nums[1])) || judgePoint24_3(float64(nums[0])-float64(nums[3]), float64(nums[2]), float64(nums[1])) || judgePoint24_3(float64(nums[0])*float64(nums[3]), float64(nums[2]), float64(nums[1])) || judgePoint24_3(float64(nums[0])/float64(nums[3]), float64(nums[2]), float64(nums[1])) || judgePoint24_3(float64(nums[3])-float64(nums[0]), float64(nums[2]), float64(nums[1])) || judgePoint24_3(float64(nums[3])/float64(nums[0]), float64(nums[2]), float64(nums[1])) || judgePoint24_3(float64(nums[2])+float64(nums[3]), float64(nums[0]), float64(nums[1])) || judgePoint24_3(float64(nums[2])-float64(nums[3]), float64(nums[0]), float64(nums[1])) || judgePoint24_3(float64(nums[2])*float64(nums[3]), float64(nums[0]), float64(nums[1])) || judgePoint24_3(float64(nums[2])/float64(nums[3]), float64(nums[0]), float64(nums[1])) || judgePoint24_3(float64(nums[3])-float64(nums[2]), float64(nums[0]), float64(nums[1])) || judgePoint24_3(float64(nums[3])/float64(nums[2]), float64(nums[0]), float64(nums[1])) || judgePoint24_3(float64(nums[1])+float64(nums[2]), float64(nums[0]), float64(nums[3])) || judgePoint24_3(float64(nums[1])-float64(nums[2]), float64(nums[0]), float64(nums[3])) || judgePoint24_3(float64(nums[1])*float64(nums[2]), float64(nums[0]), float64(nums[3])) || judgePoint24_3(float64(nums[1])/float64(nums[2]), float64(nums[0]), float64(nums[3])) || judgePoint24_3(float64(nums[2])-float64(nums[1]), float64(nums[0]), float64(nums[3])) || judgePoint24_3(float64(nums[2])/float64(nums[1]), float64(nums[0]), float64(nums[3])) || judgePoint24_3(float64(nums[1])+float64(nums[3]), float64(nums[2]), float64(nums[0])) || judgePoint24_3(float64(nums[1])-float64(nums[3]), float64(nums[2]), float64(nums[0])) || judgePoint24_3(float64(nums[1])*float64(nums[3]), float64(nums[2]), float64(nums[0])) || judgePoint24_3(float64(nums[1])/float64(nums[3]), float64(nums[2]), float64(nums[0])) || judgePoint24_3(float64(nums[3])-float64(nums[1]), float64(nums[2]), float64(nums[0])) || judgePoint24_3(float64(nums[3])/float64(nums[1]), float64(nums[2]), float64(nums[0])) } func judgePoint24_3(a, b, c float64) bool { return judgePoint24_2(a+b, c) || judgePoint24_2(a-b, c) || judgePoint24_2(a*b, c) || judgePoint24_2(a/b, c) || judgePoint24_2(b-a, c) || judgePoint24_2(b/a, c) || judgePoint24_2(a+c, b) || judgePoint24_2(a-c, b) || judgePoint24_2(a*c, b) || judgePoint24_2(a/c, b) || judgePoint24_2(c-a, b) || judgePoint24_2(c/a, b) || judgePoint24_2(c+b, a) || judgePoint24_2(c-b, a) || judgePoint24_2(c*b, a) || judgePoint24_2(c/b, a) || judgePoint24_2(b-c, a) || judgePoint24_2(b/c, a) } func judgePoint24_2(a, b float64) bool { return (a+b < 24+1e-6 && a+b > 24-1e-6) || (a*b < 24+1e-6 && a*b > 24-1e-6) || (a-b < 24+1e-6 && a-b > 24-1e-6) || (b-a < 24+1e-6 && b-a > 24-1e-6) || (a/b < 24+1e-6 && a/b > 24-1e-6) || (b/a < 24+1e-6 && b/a > 24-1e-6) }
由於程式碼過於硬核,
我們直接擊敗100%的對手:
(沒想到吧!程式碼還可以這麼寫~)
本期的題目應該都能看懂嗎?
大家還有其他的方法來得到答案嗎?
評論區留下你的想法吧!
來源:宜信技術學院
小浩:宜信科技中心攻城獅一枚,熱愛演算法,熱愛學習,不拘泥於枯燥程式設計程式碼,更喜歡用輕鬆方式把問題簡單闡述,希望喜歡的小夥伴可以多多關注!
原文首發於:「小浩演算法」
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69918724/viewspace-2677241/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 學會Lambda,讓程式Pythonic一點Python
- 【AppStore】一文讓你學會IOS應用上架AppstoreAPPiOS
- 你真的會判斷 _objc_msgForward_stret 嗎OBJForward
- 判斷難點
- Java事務註解:讓你的程式碼如絲般順滑Java
- js程式碼判斷瀏覽器JS瀏覽器
- 讓微信小程式開發如魚得水微信小程式
- 讓Nginx支援if多重判斷方法Nginx
- C語言判斷素數,判斷質素演算法C語言演算法
- 如何用LSTMs做預測?(附程式碼)| 博士帶你學LSTM
- 小程式會讓Hybrid App崛起嗎APP
- 程式碼優化-多型代替IF條件判斷優化多型
- 判斷一個數是否為質數(程式碼)
- 程式碼如人
- 小姐姐帶你一起學:如何用Python實現7種機器學習演算法(附程式碼)Python機器學習演算法
- 一文教會你如何寫複雜業務程式碼
- 人人都能學會的python程式設計教程8:條件判斷與迴圈Python程式設計
- (IOS)判斷密碼格式iOS密碼
- CRC 自動判斷大端 小端
- 下拉選項,一個小判斷
- Android Color 判斷色值小結Android
- 讓你一場 Chat 學會 GitGit
- 一張圖讓你學會LVMLVM
- 演算法之複雜度判斷演算法複雜度
- 一文讓你學完C++,乾貨收藏!!!C++
- Java基礎程式碼季節判斷季節1Java
- 微信小程式內判斷是否關注公眾號(JAVA)微信小程式Java
- 三分鐘讓你快速學會BI專案的評估與診斷
- 十幾行程式碼就可以讓你的微信小程式掛掉行程微信小程式
- 學會這個工具的使用,讓你快速生成驗證碼
- js判斷dom節點是否存在JS
- 一文詳解Python字串條件判斷方法Python字串
- JAVA編寫的斷點續傳小程式Java斷點
- 如何判斷一個點在地圖上?如何判斷一個點在多邊形內?地圖
- 一文學會如何在Keras中開發LSTMs(附程式碼)Keras
- @程式設計師,一文讓你掌握Python爬蟲!程式設計師Python爬蟲
- 一文了解低程式碼與無程式碼:異同點
- Python判斷海域有多少個小島Python