ones
时间限制:
1000 ms | 内存限制:
65535 KB
难度:
3
-
描述
- Given a positive integer N (0<=N<=10000), you are to find an expression equals to N using only 1,+,*,(,). 1 should not appear continuously, i.e. 11+1 is not allowed.
-
输入
- There are multiple test cases. Each case contains only one line containing a integer N 输出
- For each case, output the minimal number of 1s you need to get N. 样例输入
-
2 10
样例输出
-
2 7
//最优子结构就是,21调用3*7的时候7已经是最优状态,然后直接调用
//不是完全理解
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int dp[10010]={0};
int main()
{
dp[1]=1;
for(int i=2; i<10010; i++)
{
dp[i]=dp[i-1]+1;
for(int j=i-1; j>=1; j--)
{
if(i%j==0)
{
dp[i]=min(dp[i],dp[j]+dp[i/j]);
}
}
}
int n;
while(~scanf("%d",&n))
{
printf("%d\n",dp[n]);
}
return 0;
} //ac