高品质的音视频能力是怎样的? | Qcon 全球软件开发大会·上海站邀请函 了解详情
写点什么

一文让你学会如何用代码判断 "24" 点

  • 2020-03-26
  • 本文字数:5746 字

    阅读完需:约 19 分钟

一文让你学会如何用代码判断"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%的对手:


(没想到吧!代码还可以这么写~)



本期的题目应该都能看懂吗?


大家还有其他的方法来得到答案吗?


评论区留下你的想法吧!


小浩:宜信科技中心攻城狮一枚,热爱算法,热爱学习,不拘泥于枯燥编程代码,更喜欢用轻松方式把问题简单阐述,希望喜欢的小伙伴可以多多关注!

原文首发于:「小浩算法」


2020-03-26 18:59414

评论

发布
暂无评论
发现更多内容

kafka日志存储以及清理机制

泽睿

kafak

上架一夜遭全网封杀!阿里大牛熬夜半年手码的Java面试指南太强了

白亦杨

Java 编程 程序员 架构师

【TcaplusDB】大暑至,万物荣华

tcaplus

TcaplusDB

揭秘Hologres如何支持超高QPS在线服务(点查)场景

阿里云大数据AI技术

存储大师班 | NFS 的诞生与成长

QingStor分布式存储

分布式存储 分布式文件存储 NAS NFS

Jar 组件自动化风险监测和升级实践

Qunar技术沙龙

安全 风险管理

解决智慧城市发展困扰:Web3D智慧环卫GIS系统

一只数据鲸鱼

GIS 数据可视化 智慧城市 智慧环卫

金钟罩还是铁布衫?TcaplusDB定时备份助你万无一失

tcaplus

TcaplusDB

还在使用NoSQL数据库?为IoT选择TSDB

数据库 大数据 时序数据库 tsdb 数据智能

丰富 TF Serving 生态,爱奇艺开源灵活高性能的推理系统 XGBoost Serving

爱奇艺技术产品团队

开源 优化 tensorflow serving 推理

数字人民币如何普惠百姓?建行发布新金融数字便民计划

CECBC

从零开始学习3D可视化之数据对接(3)

森友小锘

大前端 数据 物联网 可视化 数字孪生

实战-使用 SSM 工具创建可动态扩容的存储池

学神来啦

Linux 运维 ssm Linux教程

【入门必读】《TcaplusDB数据库常见问题解决及诊断技巧集锦-数据库使用类-1》

TcaplusDB

nosql Data TcaplusDB tencendb

升级数据库游戏不停服?来看看TcaplusDB是怎么操作的(3)

TcaplusDB

nosql Data TcaplusDB tencendb

金钟罩还是铁布衫?TcaplusDB定时备份助你万无一失!

TcaplusDB

数据库 nosql Data TcaplusDB

【入门必读】《TcaplusDB数据库常见问题解决及诊断技巧集锦-信息咨询类-2》

TcaplusDB

nosql Data TcaplusDB tencendb

百度AI寻人获评《新周刊》2021年度公益项目

百度大脑

人工智能 寻人

Ipfs国家认可吗?国家对ipfs区块链是什么政策?

区块链 分布式存储 IPFS fil

技术解码 | 腾讯云SRT弱网优化

腾讯云音视频

TcaplusDB | 大暑,万物荣华

数据人er

数据库 nosql tencentdb TcaplusDB

细节分析Linux中五种IO模型和三种实现方式

Linux服务器开发

网络编程 epoll Linux服务器开发 Linux后台开发 IO模型

多鲸资本发布《2021教育实时音视频行业报告》:拍乐云以质取胜做行业引领者

拍乐云Pano

升级数据库游戏不停服?来看看TcaplusDB怎么操作(3)

tcaplus

数据库 TcaplusDB

TcaplusDB君 · 行业新闻汇编(7月20日)

TcaplusDB

nosql Data TcaplusDB tencendb

TcaplusDB君 | 行业新闻汇编(7月20日)

tcaplus

TcaplusDB

一文让你学会如何用代码判断&#34;24&#34;点_文化 & 方法_小浩_InfoQ精选文章