HDU 1248 寒冰王座 完全背包

来源:互联网 发布:软件测试实例视频 编辑:程序博客网 时间:2024/05/09 04:12

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

这是道简单的完全背包问题,背包问题大致分三种吧:0-1背包,完全背包以及多重背包。

完全背包和0-1背包不同,0-1背包的意见物品只有两种选择,放或者不放;但完全背包可以放0件,放1件,放2件……放n件。

如果要弄懂所有的背包问题,建议看看CSDN里一位大牛整理出来的《背包九讲》。给个链接:http://download.csdn.net/detail/wtq493841534/2583460


#include<stdio.h>#include<string.h>int dp[10001];int cost[3] = {150, 200, 350};int max(int x, int y){    return x > y ? x : y;}int main(){    int t, n, i, j;    scanf("%d", &t);    while( t-- )    {        scanf("%d", &n);        memset(dp, 0, sizeof(dp));        for(i=0; i<3; i++)        {            for(j = cost[i]; j <= n; j++)            {                dp[j] = max(dp[j], dp[j - cost[i]] + cost[i]);            }        }        printf("%d\n", n - dp[n]);    }    return 0;}


原创粉丝点击