给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化。 返回你可以获得的最大乘积。
例如,给定 n = 2,返回1(2 = 1 + 1);给定 n = 10,返回36(10 = 3 + 3 + 4)。
注意:你可以假设 n 不小于2且不大于58。

详见:https://leetcode.com/problems/integer-break/description/

C++:

class Solution {
public:
    int integerBreak(int n) {
        if(n==2||n==3)
        {
            return n-1;
        }
        int res=1;
        while(n>4)
        {
            res*=3;
            n-=3;
        }
        return res*n;
    }
};

 参考:https://www.cnblogs.com/grandyang/p/5411919.html

相关文章:

  • 2021-07-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-21
  • 2021-12-02
  • 2021-05-20
  • 2021-06-30
猜你喜欢
  • 2021-09-01
  • 2021-08-09
  • 2021-11-18
  • 2021-12-11
相关资源
相似解决方案