题目:
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).
思路:
令res[n]为n对应的最大积。那么递推方程就是:res[n]=max(i*res[n-i],i*(n-i))(其中i从1到n-1)。
程序:
class Solution {
public:
int integerBreak(int n) {
vector<int> res(n + 1,1);
for(int i = 2;i <= n;i++)
{
for(int j = 1;j < i;j++)
{
int r = max(j * res[i - j],j * (i - j));
res[i] = max(res[i],r);
}
}
return res[n];
}
};
探讨如何通过算法寻找将正整数拆分为至少两个正整数之和时所能获得的最大乘积,并提供了一个使用动态规划方法实现的C++程序示例。
1195

被折叠的 条评论
为什么被折叠?



