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
-
让你算最少的1来表示n
-
#include<stdio.h> #include<iostream> using namespace std; int dp[10000]= {0,1,2,3,4,5}; void we() { for(int i=6; i<=10000; i++) { dp[i]=dp[i-1]+1;//素数的话就在它前一个数的基础上加1,偶数的话就是它前面的基础上加1(但是偶数时不保证此时就是最小的,还需要比较) for(int j=2; j<i; j++) { if(i%j==0)//当有因数的时候用就可以写成乘积的形式,此时和它因数的dp【j】+dp【i/j】来进行比较 { dp[i]=min(dp[i],dp[j]+dp[i/j]); } } } } int main() {we();//需要放在这,放在while里面会超时 int n; while(scanf("%d",&n)!=-1) { printf("%d\n",dp[n]); } }