zoukankan      html  css  js  c++  java
  • Coins

    Description

    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.

    Input

    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.

    Output

    For each test case output the answer on a single line.

    Sample Input

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

    Sample Output

    8
    4



    多重背包(MultiplePack):
    有N种物品和一个容量为V的背包。第i种物品最多有n[i]件可用,每件费用是c[i],价值是w[i]。
    求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大。

    代码如下:
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    using namespace std;
    const int maxn=1100;
    int val[100005],num[100005];
    int f[100003],n,v;
    int min(int a,int b)
    {
        return a<b?a:b;
    }
    int main()
    {
        while (scanf("%d%d",&n,&v)!=EOF&&(n+v))
        {
            for(int i=1;i<=n;i++)
                scanf("%d",&val[i]);
            for(int i=1;i<=n;i++)
            scanf("%d",&num[i]);
            memset(f,0,sizeof(f));
            f[0]=1;
            for(int i=1;i<=n;i++)
            {
                int x=val[i];
                int  y=num[i];
                int t=1;
                while (y)
                {
                    for(int j=v;j>=x*t;j--)
                    if(f[j-x*t])
                    f[j]=1;
                    y-=t;
                    t=min(y,2*t);
                }
            }
            int ans=0;
            for(int i=1;i<=v;i++)
            ans+=f[i];
            printf("%d
    ",ans);
        }
        return 0;
    }
    

      

    
    
  • 相关阅读:
    Linux下如何从mysql数据库里导出导入数据
    安装好Pycharm后如何配置Python解释器简易教程
    Windows离线安装Python第三方库的方法
    时间输入框的测试方法
    doc转html
    pdf转png图片
    html转pdf
    html转pdf
    复习 注解反射
    Mybatis实现插入数据的时候将主键赋值给对象的两种方法
  • 原文地址:https://www.cnblogs.com/llfj/p/5768131.html
Copyright © 2011-2022 走看看