“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%的对手:
(没想到吧!代码还可以这么写~)
本期的题目应该都能看懂吗?
大家还有其他的方法来得到答案吗?
评论区留下你的想法吧!
小浩:宜信科技中心攻城狮一枚,热爱算法,热爱学习,不拘泥于枯燥编程代码,更喜欢用轻松方式把问题简单阐述,希望喜欢的小伙伴可以多多关注!
原文首发于:「小浩算法」
更多内容推荐
程序设计原则:把计算过程交给计算机
设计程序的时候,应该把计算过程交给计算机。而用不同算法设计出来的程序,其效率也不同。
2020-02-13
面试题:三数之和
无
2018-10-25
RL 训练方法集锦:简介
2020-08-13
动态规划问题的思路和技巧
动态规划是算法中最常见的一类问题之一,其解题思路常常为,将大问题分解为小问题,并且建立起通过解决小问题到解决大问题的对应关系来得到最终的答案,一旦找了分解问题和合并小问题答案的方程,问题本身就迎刃而解。
2020-05-03
结课测试 | 这些数学知识你都掌握了吗?
课程结束了,你是否全部掌握了呢?来测试一下你的水平吧!
2020-04-14
运算符
2019-03-04
大部分教程不会告诉你的 12 个 JS 技巧
在这篇文章中,作者将分享12个非常有用的JavaScript技巧,可以帮助你写出简洁且高性能的代码。
运算符与中缀 : 定义你的专有运算符
无
2018-07-26
理解递归与动态规划
数据如何切分
推荐阅读
前端之算法(九)回溯算法
2021-08-19
面试题:N 皇后问题的另一种解法
2018-11-11
前端之算法(七)动态规划
2021-08-17
Python 之斐波那契数列的实现
2022-09-30
判断与循环:给你的程序加上处理逻辑
2020-01-09
推荐的 Exploit 和 Explore 算法之二:UCB 算法
2018-03-21
SICP 习题解答 1.11
2021-03-09
电子书
大厂实战PPT下载
换一换 温铭 | API7.ai 联合创始人 & CEO
杨磊 | 滴普科技 FastData 产品线总裁
李静 | 阿里巴巴 文娱集团资深算法专家
评论