首页 > 专用服务器 > 游戏服务器

算法:如何运用算法实现“24点”数学游戏?

游戏服务器 游戏服务器 2021-09-12 13:18:33 0 算法

“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 。题目分析

拿到题目,第一反应就可以想到暴li求解。如果我们要判断给出的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%的对手:

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

算法:如何运用算法实现“24点”数学游戏?

全能高配香港物理服务器租用选购方向

如今很多企业建站都会租用香港物理服务器,对于大型的网站建设来说,如外贸网站、电商网站等,都希望能够租用到配置高、性能好的全能香港物理服务器。那么,在众多的香港物理服务器当中,哪些是全能高配的服务器?咱们又该如何选择呢?

其实,全能高配性能的香港物理服务器考察的要素有很多,它直接影响网站的正常运营。这里从以下几个方面来分析全能高配性能的香港物理服务器选择技巧,希望对大家有所帮助。

1、CPU类型

香港物理服务器的CPU类型选择非常重要,一般的网页请求、数据库运行等都需要CPU来处理,现在全能高性能的香港物理服务器都是采用双核或多核CPU。因此,大家在选择CPU类型时,一定要选择好点的CPU。

2、内存大小

香港物理服务器的内存大小会影响香港服务器处理命令的速度。当同时需要处理多条命令时,需要更高的内存,否则运行气啦会非常卡,而且也会影响访问速度。一般全能高配性能的香港物理服务器内存大小起码是8G以上的,过低的话肯定不好。

3、磁盘空间

香港物理服务器的磁盘大小也要考虑,毕竟它是放置网站文件与数据库的地方。特别是对于大型网站来说,存储的数据有很多,这就要求香港物理服务器的磁盘空间一定要够大。目前,常见的磁盘存储类型有RAID磁盘阵列、HDD磁盘存储等,建议大家选择1TB磁盘空间。

5、带宽大小

带宽大小的选择也很重要,虽然香港距离大陆比较近,香港服务器访问速度也挺快。但香港带宽资源比较紧缺,如果带宽过小的话,在网站访问高峰时段访问会非常的慢,这势必会影响香港物理服务器的整体运行效果。现在香港物理服务器通常提供5M、10M大小的国际带宽。例如下载站、电商站、视频站对带宽的要求都很高,大家务必要考虑这一点。

6、高稳定性

对于香港物理服务器来说,不仅访问速度要快,稳定性好也很重要。如果香港服务器不稳定、经常宕机的话肯定对网站有不利影响。所以,大家在选择时,尽量要考虑香港物理服务器的稳定性高不高,香港机房的防护措施好不好。有条件的话,建议选择BGP、CN2线路的香港物理服务器,一方面可以保证访问速度,另一方面也能提供高稳定性。

以上从不同的方面分析了全能高配性能香港物理服务器的选择技巧,大家可以从CPU、存储功能、内存大小、带宽、稳定性等几个方面来考虑,这些都是决定香港物理服务器性能是否优越的重要条件。

全能高配性能的香港物理服务器推荐:优质服务器租用托管IDC香港服务器,性价比非常高,如果有这方面需求的用户可以去了解下,能够满足各类网站的建设需求。

版权声明

本文内容由互联网用户自发贡献,版权归用户所有,本站不为本文内容承担相关法律责任。如有侵犯您的合法权利,请联系本站进行删除。

下一篇:返回列表
留言与评论(共有 条评论)

   
验证码:

标签列表

09主机网

统计代码 | 京ICP2194458-1号

Powered By 09主机网

使用手机软件扫描微信二维码

关注我们可获取更多热点资讯