当前位置:网站首页>剑指 Offer 14- II. 剪绳子 II
剑指 Offer 14- II. 剪绳子 II
2022-07-03 12:10:00 【嗝~~~~】
剑指 Offer 14- II. 剪绳子 II
难度中等189
给你一根长度为 n 的绳子,请把绳子剪成整数长度的 m 段(m、n都是整数,n>1并且m>1),每段绳子的长度记为 k[0],k[1]…k[m - 1] 。请问 k[0]k[1]…*k[m - 1] 可能的最大乘积是多少?例如,当绳子的长度是8时,我们把它剪成长度分别为2、3、3的三段,此时得到的最大乘积是18。
答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。
示例 1:
输入: 2输出: 1解释: 2 = 1 + 1, 1 × 1 = 1
示例 2:
输入: 10输出: 36解释: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36
提示:
- 2 <= n <= 1000
思路
- 大数取余=》不能使用动态规划=》取余之后max函数就不能用来比大小了
- 剑指 Offer 14- I. 剪绳子打印结果
- 总结规律
代码
class Solution {
public:
int cuttingRope(int n) {
long res=1;
if(n==1||n==2) return 1;
else if(n==3) return 2;
//拆解成几个3
while(n/3>1){
res*=3;
res%=1000000007;
n-=3;
}
if(n%3==1) res*=4;
else res=res*3*(n%3?n%3:1);
return res%1000000007;
}
};
边栏推荐
- 十条职场规则
- Bert running error: attributeerror: module'tensorflow contrib. tpu' has no attribute 'InputPipelineConfig'
- 阿里 & 蚂蚁自研 IDE
- Everything comes to him who waits
- Method overloading and rewriting
- The solution to change the USB flash disk into a space of only 2m
- (latest version) WiFi distribution multi format + installation framework
- alright alright alright
- 并网-低电压穿越与孤岛并存分析
- [combinatorics] permutation and combination (the combination number of multiple sets | the repetition of all elements is greater than the combination number | the derivation of the combination number
猜你喜欢
studio All flavors must now belong to a named flavor dimension. Learn more
Day 1 of kotlin learning: simple built-in types of kotlin
Grid connection - Analysis of low voltage ride through and island coexistence
【数据库原理复习题】
Xctf mobile--rememberother problem solving
Xctf mobile--app2 problem solving
[review questions of database principles]
[combinatorics] permutation and combination (the combination number of multiple sets | the repetition of all elements is greater than the combination number | the derivation of the combination number
Brief introduction to mvcc
低代码平台国际化多语言(i18n)技术方案
随机推荐
Deeply understand the mvcc mechanism of MySQL
4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment
Public and private account sending prompt information (user microservice -- message microservice)
ncnn神經網絡計算框架在香柳丁派OrangePi 3 LTS開發板中的使用介紹
Xctf mobile--app2 problem solving
C graphical tutorial (Fourth Edition)_ Chapter 15 interface: interfacesamplep268
Everything comes to him who waits
Dojo tutorials:getting started with deferrals source code and example execution summary
Detailed explanation of the most complete constraintlayout in history
Swift5.7 扩展 some 到泛型参数
阿里 & 蚂蚁自研 IDE
Approve iPad, which wants to use your icloud account
十條職場規則
【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
Enable SASL authentication for memcached
【数据库原理及应用教程(第4版|微课版)陈志泊】【SQLServer2012综合练习】
A large select drop-down box, village in Chaoyang District
Method overloading and rewriting
Quickly learn member inner classes and local inner classes
最新版盲盒商城thinkphp+uniapp