题目描述:
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.
For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4).
Note: You may assume that n is not less than 2 and not larger than 58.
分析:
根据数学知识,我们知道当3连乘时,效率最高,剩余的部分特殊处理即可。
代码如下:
int integerBreak(int n) {
vector<int>num(60,0);
num[2]=1;
num[3]=2;
num[4]=4;
for(int i=5;i<60;i++){
if(i%3==1){
num[i]=num[i-4]*4;
if(num[i-4]==2)
num[i]=12;
}
if(i%3==2){
num[i]=num[i-2]*2;
if(num[i-2]==2)
num[i]=6;
}
if(i%3==0){
num[i]=num[i-3]*3;
if(num[i-3]==2)
num[i]=9;
}
}
return num[n];
}
};