NYOJ 708 ones(try again)

ones
시간 제한:
1000ms | 메모리 제한:
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
#include
#include
#include

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
",dp[n]); } return 0; } //ac

좋은 웹페이지 즐겨찾기