游客 Signup | Login
中文 | En

3737 - 贵州大学第五届程序设计竞赛 整数分解

给定一个正整数N,可以分解成若干个正整数之和,求各种分解得到的整数最大可能的乘积。例如5可以分解成5=55=1+45=2+35=1+1+35=1+2+25=1+1+1+25=1+1+1+1+1,其中2*3=6是分解得到的数乘积最大的。

Input

输入包括多组测试数据。每组测试数据一行,包含一个待分解的正整数N(0<=N<=1000)N0表示输入结束。

Output

对每组测试数据,输出对应分解得到的最大乘积。

Examples

Input

5
6
0

Output

6
9

Solution C++

#include <stdio.h>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <string.h>
using namespace std;
int dp[1005];
int max(int a,int b)
{
	if(a>b)
		return a;
	else
		return b;
}
int main()
{
	 dp[1]=1;
	 dp[2]=2;
	 dp[3]=3;
	 dp[4]=4;
	 dp[5]=6;
	 int i,j;
	 int maxa=-1;
	 for( i=6;i<=1000;i++)
		 for( j=1;j<i;j++)
		 {
			 maxa=max(maxa,dp[j]*(i-j));
			 dp[i]=maxa;
		 }
     int n;
	 while(scanf("%d",&n)!=EOF)
	 {
		 if(n==0)
			 break;
		 printf("%d\n",dp[n]);
	 }
	 return 0;
}
Time Limit 1 second
Memory Limit 128 MB
Discuss Stats
上一题 下一题