难度:Medium
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).
我们容易发现,任何数减去3之后于3相加,都要比原本的数字要大,而这是最小的满足条件的质数,于是可以得到答案。
代码如下:
int integerBreak(int n) {
if(n==1)
return 1;
if(n==2)
return 1;
if(n==3)
return 2;
if(n==4)
return 4;
if(n==5)
return 6;
if(n==6)
return 9;
return 3*integerBreak(n-3);
}