1、点击打开链接
2.
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.
Hint:
There is a simple O(n) solution to this problem.You may check the breaking results of n ranging from 7 to 10 to discover the regularities. 3、 class Solution { public: int integerBreak(int n) { int sum=0,t,x; if(n==2) sum=1; else if(n==3) sum=2; else if(n%3==0){ sum=pow(3,n/3); } else if(n%3==1){ sum=pow(3,n/3-1)*2*2; } else sum=pow(3,n/3)*2; return sum; } };4、这样做的原因,就是列出一堆书数,然后就看出这样子满足了。理论上:点击打开链接,点击打开链接
A、首先假如有一个因子是x>4,把它分解的话(x-2)*2>4,故而令x分解的所有因子都应当<=4》,当x=4时,可分解课不分解,可认为x的所有因子<4时,依旧会出现最大值;
B、当如果由一个因子为1,当x>3时,则(x-2)*2>(x-1)*1,即不能出现因子1;
C、综上所述,x>=4时,x的所有因子由2,3组成会出现最大值;
D、当出现2的次数>2时,有3*3>2*2*2,(注意3+3=2+2+2),故而2的次数<=2会有最大;
E、x%3==0,x%3==1,x%3==2,在x>=4的情况下,任意x都可以被2,3表示出来。