首页 > ACM题库 > HDU-杭电 > HDU 2844-Coins-背包问题-[解题报告]HOJ
2014
02-17

HDU 2844-Coins-背包问题-[解题报告]HOJ

Coins

问题描述 :

Whuacmers use coins.They have coins of value A1,A2,A3…An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn’t know the exact price of the watch.

You are to write a program which reads n,m,A1,A2,A3…An and C1,C2,C3…Cn corresponding to the number of Tony’s coins of value A1,A2,A3…An then calculate how many prices(form 1 to m) Tony can pay use these coins.

输入:

The input contains several test cases. The first line of each test case contains two integers n(1 ≤ n ≤ 100),m(m ≤ 100000).The second line contains 2n integers, denoting A1,A2,A3…An,C1,C2,C3…Cn (1 ≤ Ai ≤ 100000,1 ≤ Ci ≤ 1000). The last test case is followed by two zeros.

输出:

The input contains several test cases. The first line of each test case contains two integers n(1 ≤ n ≤ 100),m(m ≤ 100000).The second line contains 2n integers, denoting A1,A2,A3…An,C1,C2,C3…Cn (1 ≤ Ai ≤ 100000,1 ≤ Ci ≤ 1000). The last test case is followed by two zeros.

样例输入:

3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0

样例输出:

8
4

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

#include <stdio.h>
#include <string.h>
int w[105];
int num[105];
int m;
int dp[100005];
int max(int a,int b)
{
    return a>b?a:b;
}
void ZeroOnePack(int v,int c)
{
    int i;
    for(i=m;i>=c;i--)
        dp[i]=max(dp[i],dp[i-c]+v);
}
void CompletePack(int v,int c)
{
    int i;
    for(i=c;i<=m;i++)
        dp[i]=max(dp[i],dp[i-c]+v);
}
int main()
{
    int n;
    while(scanf("%d%d",&n,&m),n+m)
    {
        int i;
        memset(dp,0,sizeof(dp));
        for(i=1;i<=n;i++)
            scanf("%d",&w[i]);
        for(i=1;i<=n;i++)
            scanf("%d",&num[i]);
        for(i=1;i<=n;i++)
        {
            if(num[i]*w[i]>m)
                CompletePack(w[i],w[i]);
            else
            {
                int k=1;
                while(k<=num[i])
                {
                    ZeroOnePack(k*w[i],k*w[i]);
                    num[i]=num[i]-k;
                    k=k<<1;
                }
                ZeroOnePack(num[i]*w[i],num[i]*w[i]);
            }
        }
        int s=0;
        for(i=1;i<=m;i++)
        {
            if(dp[i]==i)
                s++;
        }
        printf("%d\n",s);
    }
    return 0;
}

解题参考:http://blog.csdn.net/juststeps/article/details/8711815


  1. #include <cstdio>
    #include <cstring>

    const int MAXSIZE=256;
    //char store[MAXSIZE];
    char str1[MAXSIZE];
    /*
    void init(char *store) {
    int i;
    store['A']=’V', store['B']=’W',store['C']=’X',store['D']=’Y',store['E']=’Z';
    for(i=’F';i<=’Z';++i) store =i-5;
    }
    */
    int main() {
    //freopen("input.txt","r",stdin);
    //init(store);
    char *p;
    while(fgets(str1,MAXSIZE,stdin) && strcmp(str1,"STARTn")==0) {
    if(p=fgets(str1,MAXSIZE,stdin)) {
    for(;*p;++p) {
    //*p=store[*p]
    if(*p<’A’ || *p>’Z') continue;
    if(*p>’E') *p=*p-5;
    else *p=*p+21;
    }
    printf("%s",str1);
    }
    fgets(str1,MAXSIZE,stdin);
    }
    return 0;
    }