leetcode 343. Integer Break

来源:互联网 发布:网络导航 编辑:程序博客网 时间:2024/05/15 03:07

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.

思路:对于正数a1+a2+...+ak=n,求max(a1*a2*...*ak),当且仅当a1=a2=...+ak时取最大值,这里是整数,变一下就行。


int integerBreak(int n) {int max = 0;for (int i = 2; i <= n; i++){int yy = 1;int p = n / i;int k = n - i*p;yy = pow(p, i- k);yy *= pow(p+1,k);if (yy > max)max = yy;}return max;}

accept

0 0
原创粉丝点击