POJ 1787

来源:互联网 发布:ps淘宝美工教程全集 编辑:程序博客网 时间:2024/05/21 08:36

Charlie's Change
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 2896 Accepted: 810

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 216 0 0 0 10 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分,现在给出咖啡的金额,求出能够恰好买一杯咖啡,并且能够花出的硬币数最多的方案。


思路:动态规划的多重背包,背包容量V表示能够支付恰好V分钱所能花出的硬币的最大数量。打印方案可以多开一个数组,也可以直接把状态转移的数组类型写成结构体,对于每个状态都记录下当前状态的各种硬币的花费情况。状态转移时不要忘了当前枚举的硬币以前的硬币的数量都要变成和转移成该状态的状态的硬币数一样。

下面是代码:

#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <ctype.h>#include <cstring>#include <string>#include <queue>#include <cmath>#define MAXN 10010#define INF 2100000000#define pu system("pause")#pragma comment(linker, "/STACK:16777216");using namespace std;int sum, num[4];int cost[4] = { 1, 5, 10, 25 };class node{    public:    int a[4];    int val;    node()    {        for(int i = 0; i < 4; i++) a[i] = 0;        val = -INF;    }};int main(){    //freopen("C:/Users/Admin/Desktop/in.txt", "r", stdin);    while(cin >> sum >> num[0] >> num[1] >> num[2] >> num[3] && sum != 0)    {        node dp[MAXN];        dp[0].val = 0;        for(int i = 0; i < 4; i++)        {            int amount = 1;            while(num[i]-amount > 0)            {                for(int j = sum; j >= cost[i]*amount; j--)                {                    if(dp[j-amount*cost[i]].val != -INF)                    if(dp[j].val < dp[j-amount*cost[i]].val+amount)                    {                        dp[j].a[i] = dp[j-amount*cost[i]].a[i]+amount;                        dp[j].val = dp[j-amount*cost[i]].val+amount;                        for(int k = 0; k < i; k++) dp[j].a[k] = dp[j-amount*cost[i]].a[k];                    }                }                num[i] -= amount;                amount *= 2;            }            for(int j = sum; j >= cost[i]*num[i]; j--)            {                if(dp[j-num[i]*cost[i]].val != -INF)                if(dp[j].val < dp[j-num[i]*cost[i]].val+num[i])                {                    dp[j].a[i] = dp[j-num[i]*cost[i]].a[i]+num[i];                    dp[j].val = dp[j-num[i]*cost[i]].val+num[i];                    for(int k = 0; k < i; k++) dp[j].a[k] = dp[j-amount*cost[i]].a[k];                }            }        }        if(dp[sum].val > -INF)        {            printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",            dp[sum].a[0], dp[sum].a[1], dp[sum].a[2], dp[sum].a[3]);        }        else        {            printf("Charlie cannot buy coffee.\n");        }    }    return 0;}



0 0