当前位置:网站首页>《剑指Offer》剪绳子
《剑指Offer》剪绳子
2022-07-27 14:15:00 【傻子是小傲娇】
题目描述
给你一根长度为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
乘法计算,除了一以外,越多的数相乘结果越大
从2开始考虑,如果用2分段,存在奇数最后会留下一个2,1,这时
应该换为3,因此2,3分段
2*2*2<3*3,应该让3尽可能的多一些
用数%3,余数为2则存在一个2,为1则存在两个2class Solution { public: int cutRope(int target) { if(target<=3)return target-1; int res=1;//%3=0 if(target%3==1){ res=4; target-=4; }else if(target%3==2){ res=2; target-=2; } while(target){ res*=3; target-=3; } return res; } };
边栏推荐
- 修改frameworks资源文件如何单编
- 如何做好企业系统漏洞评估
- LeetCode 341.扁平化嵌套列表迭代器 dfs,栈/ Medium
- LeetCode 81. 搜索旋转排序数组 II 二分/medium
- 同花顺开户在手机开户安全吗?
- 视觉系统设计实例(halcon-winform)-10.PLC通讯
- Dialog manager Chapter 3: create controls
- NEFU118 n! How many zeros are there after [basic theorem of arithmetic]
- 网络设备硬核技术内幕 路由器篇 7 汤普金森漫游网络世界(下)
- Wechat applet realizes music search page
猜你喜欢

什么是Tor?Tor浏览器更新有什么用?

Graphic SQL of giant image

FPGA timing constraint sharing 04_ Output delay constraint

Visual system design example (Halcon WinForm) -9. text display

移动端使用vantUI的list组件,多个tab项来回切换时,列表加载多次导致数据无法正常展示

谷歌团队推出新Transformer,优化全景分割方案|CVPR 2022

DIY制作示波器的超详细教程:(一)我不是为了做一个示波器

代码覆盖率统计神器-jacoco工具实战

The interviewer asked: how to judge whether an element is in the visible area?

LeetCode 783. 二叉搜索树节点最小距离 树/easy
随机推荐
How to help enterprises optimize office management
OBS advanced DXGI acquisition screen process, and how to modify it to its own cursor
[ManageEngine] what is Siem
Stock trading 4
internship:其他配置类的编写
Confirm the time accuracy of the power supply setting voltage through the i/o function of vn1630/vn7640
RS485接口的EMC设计方案
事务_基本演示和事务_默认自动提交&手动提交
腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?
Stm32f103c8t6 drives ssd1306 0.96 "IIC OLED display under Arduino frame
通过VN1630/VN7640的I/O功能来确认电源设置电压的时间精确度
Web page table table, realizing rapid filtering
LeetCode 74. 搜索二维矩阵 二分/medium
See "sense of security" in uncertainty Volvo asked in 2022
cap理论和base理论
同花顺开户在手机开户安全吗?
Unityui aspect processing (induction and accumulation)
FPGA timing constraint sharing 04_ Output delay constraint
MySQL save data prompt: out of range value for column error
网络设备硬核技术内幕 路由器篇 7 汤普金森漫游网络世界(下)