zoukankan      html  css  js  c++  java
  • poj 1787 Charlie's Change (多重背包可作完全背包)

    Charlie's Change
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 3792   Accepted: 1144

    Description

    Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task. 

    Your program will be given numbers and types of coins Charlie has and the coffee price. The coffee vending machines accept coins of values 1, 5, 10, and 25 cents. The program should output which coins Charlie has to use paying the coffee so that he uses as many coins as possible. Because Charlie really does not want any change back he wants to pay the price exactly. 

    Input

    Each line of the input contains five integer numbers separated by a single space describing one situation to solve. The first integer on the line P, 1 <= P <= 10 000, is the coffee price in cents. Next four integers, C1, C2, C3, C4, 0 <= Ci <= 10 000, are the numbers of cents, nickels (5 cents), dimes (10 cents), and quarters (25 cents) in Charlie's valet. The last line of the input contains five zeros and no output should be generated for it.

    Output

    For each situation, your program should output one line containing the string "Throw in T1 cents, T2 nickels, T3 dimes, and T4 quarters.", where T1, T2, T3, T4 are the numbers of coins of appropriate values Charlie should use to pay the coffee while using as many coins as possible. In the case Charlie does not possess enough change to pay the price of the coffee exactly, your program should output "Charlie cannot buy coffee.".

    Sample Input

    12 5 3 1 2
    16 0 0 0 1
    0 0 0 0 0
    

    Sample Output

    Throw in 2 cents, 2 nickels, 0 dimes, and 0 quarters.
    Charlie cannot buy coffee.


    感觉上是多重背包,实际上用完全背包的思路来做很快。

    题意:分硬币,有1,5,10,25四种硬币,给定每种硬币的数量,给定要组合成的价值,问刚好达到价值时用的硬币最多的情况。

    附上代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #define N 10010
     5 #define inf (-0x3f3f3f3f)
     6 using namespace std;
     7 int max(int a,int b)
     8 {
     9     return a>b?a:b;
    10 }
    11 int main()
    12 {
    13     int dp[N],path[N],used[N];
    14     // dp[j] 表示 j 块钱最多由多少块硬币组成,
    15     //path[j] 表示 上一次最多有多少块构成的 j 块钱,used[j] 表示 j 块钱时,已经放了多少同种类的硬币。
    16     int i,j,m,n;
    17     int num[4],val[4]= {1,5,10,25};
    18     while(~scanf("%d %d %d %d %d",&n,&num[0],&num[1],&num[2],&num[3]))
    19     {
    20         if(n==0&&num[0]==0&&num[1]==0&&num[2]==0&&num[3]==0)
    21             break;
    22         memset(dp,inf,sizeof(dp));
    23         memset(path,0,sizeof(path));
    24         path[0]=-1;
    25         dp[0]=0;
    26 
    27         for(i=0; i<4; i++)
    28         {
    29             memset(used,0,sizeof(used));
    30             for(j=val[i]; j<=n; j++)
    31             {
    32                 if(dp[j-val[i]]+1>dp[j]&&dp[j-val[i]]>=0&&used[j-val[i]]<num[i])
    33                 {
    34                     dp[j]=dp[j-val[i]]+1;
    35                     used[j]=used[j-val[i]]+1;
    36                     path[j]=j-val[i];
    37                 }
    38             }
    39         }
    40 
    41         int ans[100];
    42         memset(ans,0,sizeof(ans));
    43         if(dp[n]<0)
    44         {
    45             printf("Charlie cannot buy coffee.
    ");
    46         }
    47         else
    48         {
    49             while(path[n]!=-1)
    50             {
    51                 ans[n-path[n]]++;
    52                 n=path[n];
    53             }
    54             printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.
    ", ans[val[0]], ans[val[1]], ans[val[2]], ans[val[3]]);
    55         }
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    使用UIImagePickerController时3DTouch引起的Crash问题的解决--备用
    阿帕奇证书配置
    终端编写c程序
    native2ascii 在 Mac终端的转码
    mac 下 配置 阿帕奇
    苹果电脑的坑
    iOS证书快要过期怎么办?
    An unspecified error occurred!
    一个苹果证书怎么多次使用
    多媒体开发之---live555的多线程支持,原本只是单线程,单通道
  • 原文地址:https://www.cnblogs.com/pshw/p/5173374.html
Copyright © 2011-2022 走看看