Ones
Description
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.
Input
There are multiple test cases. Each case contains only one line containing a integer N
Sample Input
2
10
10
Sample Output
2
7
代码:
7
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#define mem(a,b) memset(a,b,sizeof a)
using namespace std;
const int N=10010;
int dp[N];
int main()
{
int n;
int tmp;
for(int i=0;i<=N;i++)
{
dp[i]=i;
for(int j=2;j*j<=n;j++)
{
tmp=dp[i%j]+dp[i/j]+dp[j];
if(tmp<dp[i])
dp[i]=tmp;
}
}
while(scanf("%d",&n)==1)
{
printf("%d\n",dp[n]);
}
return 0;
}