Integer Break

来源:互联网 发布:mysql递归 编辑:程序博客网 时间:2024/05/19 18:11

一、问题描述

二、思路

根据

(N/2)*(N/2)>=N, then N>=4

(N-1)/2 *(N+1)/2>=N, then N>=5

这两个表达式意味着因子应该小于4,最好是3,其次是2.

例如:6 : 3*3 > 2*2*2

三、代码

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


0 0
原创粉丝点击