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;
    }
    

      

    
    
  • 相关阅读:
    6)从左到右依次递增,从上到下依次递减的数组
    2)PHP代码运行过程
    1)session总结
    3)ARP到底属于网络层还是链路层
    (2)链表中的一级指针和二级指针的问题
    javascript 封装ajax(多版本)
    javaScript中的两个叹号(!!)
    css获取背景中指定的位置
    解析this.initialize.apply(this, arguments);
    ul中嵌套div导致布局混乱
  • 原文地址:https://www.cnblogs.com/llfj/p/5768131.html
Copyright © 2011-2022 走看看