Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
对一个自然数N ( 1 <= N <= 50 ) ,N可以分解成若干个数字(数字可以是1,2,3,….,9)之和,问题是如何分解能使这些数字的乘积最大。
Input
输入数据有多组,每组占一行,每行包含一个自然数N(1 <= N <= 50)。输入文件直到EOF为止!
Output
对每组输入,输出有2行。第一行是N分解出的所有数字,以空格分隔,最后一个数字后也有空格;第二行是N分解出的所有数字的个数、乘积。
Example Input
20
24
28
Example Output
3 3 3 3 3 3 2
7 1458
3 3 3 3 3 3 3 3
8 6561
3 3 3 3 3 3 3 3 4
9 26244
Hint
由数学知识可知,只有把N分成尽可能多的3,它们的乘积才能最大(当只剩下4时不用再分,因为: 4 > 3*1)
Author
zlh
#include<stdio.h>
#include<string.h>
int count, a[111], i = 0;
void fenjie(int n)
{
if(n > 4)
{
n = n - 3;
a[i++] = 3;
count++;
fenjie(n);
}
else
{
count++;
a[i++] = n;
}
}
int main()
{
int n;
while(~scanf("%d", &n))
{
i = 0, count = 0;
int x = 1;
memset(a, 0, sizeof(a));
fenjie(n);
for(i = 0; i < count; i++)
{
printf("%d ", a[i]);
x *= a[i];
}
printf("\n%d %d\n", count, x);
}
return 0;
}
转载请注明原文地址: https://ju.6miu.com/read-50394.html