标签:col break ret color 拆分 public bre 必须 code
1 //以3来进行划分 2 class Solution 3 { 4 public: 5 int integerBreak(int n) 6 { 7 if(n <= 3) return n - 1; //必须切一刀 8 //n >= 5 2*(n-2) > n 3*(n-3) > n 且3*(n-3) >= 2*(n-2) 9 //n = 4 2 * 2 > 1 * 3 10 //2和3不能再分了 分了就变小了 且3优于2 11 int a = n / 3; 12 int b = n % 3; 13 if(b == 0) 14 { 15 //全部分成3 16 return pow(3, a); 17 } 18 if(b == 1) 19 { 20 //n = 10 3 * 3 * 3 * 1 ==> 3 * 3 * 2 * 2 21 //4 1 和 3 要变成2 * 2 22 return pow(3, a-1) * 4; 23 } 24 //b == 2 2不能再分了 25 return pow(3, a) * 2; 26 } 27 };
标签:col break ret color 拆分 public bre 必须 code
原文地址:https://www.cnblogs.com/yuhong1103/p/12755161.html