首页 > 代码库 > leetcode343

leetcode343

public class Solution {
    public int IntegerBreak(int n) {
        if (n == 2)
            {
                return 1;
            }
            else if (n == 3)
            {
                return 2;
            }
            var max = int.MinValue;
            for (int i = 2; i <= n / 2; i++)
            {
                var div = n / i;
                var mod = n % i;
                var cur = 0;
                if (mod == 0)
                {
                    cur = Convert.ToInt32(Math.Pow(i, div));
                }
                else
                {
                    var cur1 = Convert.ToInt32(Math.Pow(i, div - 1) * (i + mod));
                    var cur2 = Convert.ToInt32(Math.Pow(i, div)) * mod;
                    cur = Math.Max(cur1, cur2);
                }
                if (cur > max)
                {
                    max = cur;
                }
                else
                {
                    break;
                }
            }
            return max;
    }
}

https://leetcode.com/problems/integer-break/#/description

leetcode343