Go语言实现:【剑指offer】剪绳子
2024-09-06 03:48:37
该题目来源于牛客网《剑指offer》专题。
给你一根长度为n的绳子,请把绳子剪成整数长的m段(m、n都是整数,n>1并且m>1),每段绳子的长度记为k[0],k[1],…,k[m]。请问k[0]xk[1]x…xk[m]可能的最大乘积是多少?例如,当绳子的长度是8时,我们把它剪成长度分别为2、3、3的三段,此时得到的最大乘积是18。
输入一个数n,意义见题面。(2 <= n <= 60)
输出答案。
Go语言实现:
func cutRope(n int) int {
if n<2 || n > 60 {
return 0
}
if n == 2 {
return 1
}
if n == 3 {
return 2
}
if n == 4 {
return 4
}
return 3*cutRopeHandler(n-3)
}
func cutRopeHandler(n int) int {
if n == 2 {
return 2
}
if n == 3 {
return 3
}
if n == 4 {
return 4
}
return 3*cutRopeHandler(n-3)
}
最新文章
- 微信小程序 开发 微信开发者工具 快捷键
- 旅行计划-DAG上最长路
- 理解Flight框架核心
- jQuery on()方法绑定动态元素的点击事件无效
- DirectX 9 UI三种设计学习笔记:文章4章Introducing DirectInput+文章5章Wrapping Direct3D
- 服务端套接字类CxServerSocket的使用
- AngularJs学习笔记0——前言
- NPOI导出excel(带图片)
- AsyncLocal的运作机制和陷阱
- 网络协议 3 - 从物理层到 MAC 层
- GT sport真实赛道详解 - Brands Hatch | 伯蘭士赫治GP賽車場
- input[type=file]上传文件(格式判断、文件大小、上传成功后操作)
- [转]decorators.xml的用法
- 解决pycharm添加第三方包失败
- Django的认证系统—auth模块
- suricata 原文记录
- OpenStack的架构详解[精51cto]
- jq中each的中断
- c++ 类的构造顺序
- column count of mysql.proc is wrong. expected 20,found 16. the table is probably corruptd.