思路:
3 = 1+2
4 = 2+2
5 = 2+3
6 = 3+3
7 = 2+2+3
8 = 2+3+3
9 = 3+3+3
10 = 2+2+3+3
..........
通过分解发现把一个数分解成尽可能多的2与3就为最大值。
code:
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
struct node
{
int num[1002];
int cnt;
}p;
void multipy(int c, int pow, int n)
{
int i = 0, cnt = p.cnt;
if(c >= n) return ;
for(i = 0; i<=cnt; i++)
p.num[i] *= pow;
for(i = 0; i<cnt; i++)
if(p.num[i]>9) p.num[i+1] += p.num[i]/10, p.num[i] = p.num[i]%10;
if(p.num[i]>9) p.num[i+1] += p.num[i]/10, p.num[i] = p.num[i]%10, cnt = i+1;
p.cnt = cnt;
multipy(c+1, pow, n);
}
int main()
{
//freopen("input.txt", "r", stdin);
int i = 0, j = 0, n = 0, x = 0, y = 0;
scanf("%d", &n);
if(n == 1) printf("1\n");
else
{
x = n/3;
y = (n%3)/2;
if(n%3%2) x--, y += 2;
p.cnt = 0;
memset(p.num, 0, sizeof(p.num));
p.num[0] = 3;
multipy(0, 3, x-1);
if(x <= 0) p.num[0] = 2, y--;
multipy(0, 2, y);
for(i = p.cnt; i>-1; i--)
printf("%d", p.num[i]);
printf("\n");
}
//printf("%.0lf", pow(3, x)*pow(2, y));
return 0;
}