HDU 1028 Ignatius and the Princess III 动态规划完全背包

http://acm.hdu.edu.cn/showproblem.php?pid=1028

题意:

  给你个数N,要你拆分成很多个数字的和。

 

坑爹:

  但不能出现 4  =  3 + 1 和 4 = 1 + 3 这样是算重复的。

 

解法:

  每次对一个数N进行 ( 1 - N )的放与不放的试探, 比如说5,不放5的最优解就是在与现在相同负重的时候放了4的解,

放5分就等于 f [j-5] + weight  这两种情况加起来就是 5 的最优情况。

 

View Code
 1 #include<iostream>
 2 using namespace std;
 3 
 4 const int maxn = 120 + 10;
 5 //const int INF = -1 * 0x7fffffff;
 6 int n;
 7 int f[maxn];
 8 
 9 int max(int a,int b)
10 {
11     return a > b ? a : b;
12 }
13 
14 void Complete(int cost)
15 {
16     int j;
17     for(j=cost; j<=n; j++)
18     {
19         f[j] += f[j-cost];
20     }
21 }
22 
23 int main()
24 {
25     while(cin>>n)
26     {
27         int i;
28         memset(f,0,sizeof(f));
29         f[0]=1;
30         for(i=1; i<=n; i++)
31         {
32             Complete(i);
33         }
34         cout<<f[n]<<endl;
35     }
36     return 0;
37 }

 

posted @ 2012-09-13 11:23  pc....  阅读(199)  评论(0)    收藏  举报