要求输入整数,求其分解后整数相乘的最大数
根据提示,将7-10分解,可以发现
4 = 2 * 2 === 2 + 2
5 = 3 * 2 === 3 + 2
6 = 3 * 3 === 3 + 3
7 = 3 * 4 === 3 + 4
8 = 3 * 3 * 2 = == 3 + 5
9 = 3 * 3 * 3 === 3 + 6
输入整数N的最终形式都是3*3*.....*4或者*3*2或者*2或者直接再*3
所以就要有个判断,最后得到的值根据整数是否为2,3,4,5进行讨论可得
class Solution {
public:
int integerBreak(int n) {
vector<int>vec;
int total = 1;
if(n >= 5){
int value = n - 3;
int flag = 0;
while(value >= 2){
vec.push_back(3);
if(value == 2){//如果为2 直接push
vec.push_back(2);
flag = 1;
}
else if(value == 3){//如果为3直接push
vec.push_back(3);
flag = 1;
}
else if(value == 4){
vec.push_back(4);//如果为4直接push
flag = 1;
}
else if(value == 5){//如果为5则push(3),push(2),设标志跳出循环,否则继续
vec.push_back(3);
vec.push_back(2);
flag = 1;
}
if(flag)
break;
else
value -= 3;
}
for(int i = 0;i != vec.size();++i)
total *= vec[i];
}
else if(n == 4)
total = 4;
else if(n == 3)
total = 2;
else total = 1;
return total;
}
};