# 漫画：骚操作系列（一文让你学会如何用代码判断"24"点）

24点”是一种数学游戏，正如象棋、围棋一样是一种人们喜闻乐见的娱乐活动。它始于何年何月已无从考究，但它以自己独具的数学魅力和丰富的内涵正逐渐被越来越多的人们所接受。今天就为大家分享一道关于“24点”的算法题目。

01 第679题：24点（困难题目）

02 题目分析

4个数字，3个操作符，外加括号，基本目测就能想到组合数不会大到超出边界。所以，我们只要把他们统统列出来，不就可以进行求解了吗？说干就干！

```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：表示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
}
```

```//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)
}
```

```//硬核代码，不服来辩！
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(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]))
}
```

03

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)
}
```

（没想到吧！代码还可以这么写~）

0 条评论

• ### 漫画：原地删除类题型详解（二次修订版）

题目27：给定一个数组 nums 和一个值 val，你需要原地移除所有数值等于 val 的元素，返回移除后数组的新长度。

• ### 漫画：经典鹅厂面试题（2Sum，3Sum，4Sum）

该题为 二数之和 的进阶版本，当然还有一个进阶版本为 四数之和。我们将会一一进行分析！

• ### 一道魔性的贪心题目（随意吐槽）

假设你是一位很棒（多棒？？？）的家长，想要给你的孩子们一些小饼干（不能给大饼干吗？？？）但是，每个孩子最多只能给一块饼干（有毒吧。。。）

• ### 小浩算法|一文让你学会如何用代码判断"24"点

“24点”是一种数学游戏，正如象棋、围棋一样是一种人们喜闻乐见的娱乐活动。它始于何年何月已无从考究，但它以自己独具的数学魅力和丰富的内涵正逐渐被越来越多的人们所...

• ### 【leetcode刷题】T125-求根到叶子节点数字之和

https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/

• ### Mrkdown教程

Markdown是一种轻量级标记语言，它以纯文本形式(易读、易写、易更改)编写文档，并最终以HTML格式发布。 Markdown也可以理解为将以MARKD...

• ### 如何选择优化器 optimizer

在很多机器学习和深度学习的应用中，我们发现用的最多的优化器是 Adam，为什么呢？ 下面是 TensorFlow 中的优化器， https://www.ten...