当前位置:网站首页>剑指 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;
}
};
边栏推荐
- 基于同步坐标变换的谐波电流检测
- Redhat5 installing socket5 proxy server
- 最新版盲盒商城thinkphp+uniapp
- Ali & ant self developed IDE
- Sword finger offer14 the easiest way to cut rope
- OpenStack节点地址改变
- Detailed explanation of the most complete constraintlayout in history
- Public and private account sending prompt information (user microservice -- message microservice)
- [review questions of database principles]
- Dix règles de travail
猜你喜欢

低代码平台国际化多语言(i18n)技术方案
![[ArcGIS user defined script tool] vector file generates expanded rectangular face elements](/img/39/0b31290798077cb8c355fbd058e4d3.png)
[ArcGIS user defined script tool] vector file generates expanded rectangular face elements

Idea packages the web project into a war package and deploys it to the server to run

Integer case study of packaging

【数据挖掘复习题】

Swift bit operation exercise

电压环对 PFC 系统性能影响分析

Alibaba is bigger than sending SMS (user microservice - message microservice)

GaN图腾柱无桥 Boost PFC(单相)七-PFC占空比前馈

(最新版) Wifi分销多开版+安装框架
随机推荐
idea将web项目打包成war包并部署到服务器上运行
C graphical tutorial (Fourth Edition)_ Chapter 13 entrustment: what is entrustment? P238
ORM use of node -serialize
Xctf mobile--app2 problem solving
低代码平台国际化多语言(i18n)技术方案
RedHat5 安装Socket5代理服务器
GCN thinking - word2vec directly calculates text classification
[data mining review questions]
如何在微信小程序中获取用户位置?
[network counting] Chapter 3 data link layer (2) flow control and reliable transmission, stop waiting protocol, backward n frame protocol (GBN), selective retransmission protocol (SR)
[judgment question] [short answer question] [Database Principle]
Harmonic current detection based on synchronous coordinate transformation
[review questions of database principles]
[exercise 6] [Database Principle]
Exploration of sqoop1.4.4 native incremental import feature
Analysis of the influence of voltage loop on PFC system performance
[combinatorics] permutation and combination (multiple set permutation | multiple set full permutation | multiple set incomplete permutation all elements have a repetition greater than the permutation
SLF4J 日志门面
【習題七】【數據庫原理】
initial、inherit、unset、revert和all的区别