[Medium]Integer Break

来源:互联网 发布:socket异步收发数据 编辑:程序博客网 时间:2024/05/17 03:41

问题:
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.
解法:

源码:

class Solution {public:    int integerBreak(int n) {        int f[70];        memset(f, 0, sizeof(f));        f[1] = 1;        int k;        for (int i = 2; i <= 58; ++i) {            k = 0;            for (int j = 1; j <= (i/2+1); ++j) {                k = max(k, max(f[j],j) * max(f[i-j],(i-j)));             }            f[i] = k;        }        return f[n];    }};//注意循环内的初始化
0 0
原创粉丝点击