http://acm.nyist.net/JudgeOnline/problem.php?pid=708
动态规划的题目,还是老方法,关键在于动态转移方程。
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
int dp[10010];
int minn(int a,int b)
{
a = a<b?a:b;
return a;
}
void DP()
{
dp[1] = 1;
dp[2] = 2;
int i,j;
for(i = 3; i <= 10010; i++)
{
dp[i] = i;
for(j = 1; j <i; j++)
{
dp[i] = minn(dp[i],dp[i-j] + dp[j]);
if(j != 1 && i % j == 0)
dp[i] = minn(dp[i],dp[i/j] + dp[j]);
}
}
}
int main()
{
//freopen("2.txt","w",stdout);
int n;
memset(dp,0,sizeof(dp));
DP();
while(cin>>n)
{
cout<<dp[n]<<endl;
}
return 0;
}