zoukankan      html  css  js  c++  java
  • hdu 2126

    背包,输出方案数!

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define maxn 505
    using namespace std;
    
    int f[maxn];
    int cot[maxn];
    int num[maxn];
    
    int main()
    {
        int n,m,t;
        scanf("%d",&t);
        while(t--)
        {
            int mi=1<<28;
            scanf("%d%d",&n,&m);
            for(int i=1; i<=n; i++)
            {
                scanf("%d",&num[i]);
                mi=min(mi,num[i]);
            }
            if(m<mi)
            {
                puts("Sorry, you can't buy anything.");
                continue;
            }
            memset(f,0,sizeof f);
            memset(cot,0,sizeof cot);
            for(int i=0; i<=m; i++)
                cot[i]=1;
            for(int i=1; i<=n; i++)
            {
                for(int j=m; j>=num[i]; j--)
                {
                    if(f[j]==f[j-num[i]]+1)
                        cot[j]+=cot[j-num[i]];
                    else if(f[j]<f[j-num[i]]+1)
                    {
                        cot[j]=cot[j-num[i]];
                    }
                    f[j]=max(f[j],f[j-num[i]]+1);
    //                printf("%d %d|  ",f[j],cot[j]);
                }
    //            puts("");
            }
            printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.
    ",cot[m],f[m]);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    02-print的用法
    01-Hello World
    01-查看系统整体性能情况:sar
    03-购物车
    Python之路,Day2
    02-三级菜单
    Python之路,Day1
    loadrunner中配置java脚本环境
    算法
    实现testNg的retry机制
  • 原文地址:https://www.cnblogs.com/yours1103/p/3650096.html
Copyright © 2011-2022 走看看