要使得分解后的整数乘积最大,就要选用尽可能多的3
直到剩下4或2时用2
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n; cin >> n;
if(n % 3 == 1) // 需要分解为3 * 3 * ... * 3 * 4的情况(取余为1,多的这个1给4)
// 例如:13 == 3 + 3 + 3 + 4
{
for(int i = 0; i < n / 3 - 1; i++) // 能分解成3的个数为n / 3 - 1
{
cout << "3 *" << " ";
}
cout << "4 = " << pow(3, n / 3 - 1) * 4 << endl;
}
else if(n % 3 == 2) // 需要分解为3 * 3 * ... * 3 * 2的情况(取余为2,2单独成数)
// 例如:14 == 3 + 3 + 3 + 3 + 2
{
for(int i = 0; i < n / 3; i++)
{
cout << "3 *" << " ";
}
cout << "2 = " << pow(3, n / 3) * 2 << endl;
}
else // 其他的为可以全部分解为3的情况
{
for(int i = 0; i < n / 3 - 1; i++) // -1的原因是为了统一格式,让最后一个输出的3格式正确
{
cout << "3 *" << " ";
}
cout << "3 = " << pow(3, n / 3) << endl;
}
return 0;
}