poj1787Charlie's Change(多重背包+记录路径+好题)

Charlie‘s Change

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 3720   Accepted: 1125

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.

题意:给出想要买的东西的价格p,有四枚硬币面值分别是1,5,10,25,然后给出每一种硬币的个数,求买这个东西最多需要多少枚硬币

这题很不错,num[i][j]用来记录i状态下第j枚硬币的个数,dp[i]用来记录硬币的个数;要求的就是在硬币个数最多的情况下每一种硬币的个数

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cstdio>
 5
 6 using namespace std;
 7 const int MAX = 10000 + 10;
 8 int dp[MAX],num[MAX][5],c[5],p;
 9 int w[5]={1,5,10,25};
10 void ZeroOnePage(int cost, int mount, int kind) //01背包传递参数有这一种硬币的数量和种类
11 {
12     for(int i = p; i >= cost; i--)
13     {
14         if(dp[i - cost] > -1 && dp[i] < dp[i - cost] + mount) //求枚数最多的情况,01的数量需要参数传递,完全背包就是1
15         {
16             dp[i] = dp[i - cost] + mount;
17             for(int j = 0; j < 4; j++)
18                 num[i][j] = num[i - cost][j];  //更改这一状态下每一种硬币的数量
19             num[i][kind] += mount;
20         }
21     }
22 }
23 void CompletePage(int cost, int kind)
24 {
25     for(int i = cost; i <= p; i++)
26     {
27         if(dp[i - cost] > -1 && dp[i] < dp[i - cost] + 1)
28         {
29             dp[i] = dp[i - cost] + 1;
30             for(int j = 0; j < 4; j++)
31                 num[i][j] = num[i - cost][j];
32             num[i][kind] += 1;
33         }
34     }
35 }
36 void MultiplePage(int cost,int mount,int kind)
37 {
38     if(cost * mount >= p)
39     {
40         CompletePage(cost, kind);
41         return;
42     }
43     int k = 1;
44     while(k < mount)
45     {
46         ZeroOnePage(k * cost, k, kind);
47         mount = mount - k;
48         k <<= 1;
49     }
50     if(mount > 0)
51         ZeroOnePage(mount * cost, mount, kind);
52     return ;
53 }
54 int main()
55 {
56     while(scanf("%d", &p) != EOF)
57     {
58         int sum = p;
59         for(int i = 0; i < 4; i++)
60         {
61             scanf("%d", &c[i]);
62             sum += c[i];
63         }
64         if(sum == 0)
65             break;
66         memset(num,0,sizeof(num));
67         memset(dp,-1,sizeof(dp));
68         dp[0] = 0;
69         for(int i = 0; i < 4; i++)
70             if(c[i])
71             MultiplePage(w[i],c[i],i);
72         if(dp[p] > 0)
73         {
74             printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",num[p][0],num[p][1],num[p][2],num[p][3]);
75         }
76         else
77         {
78             printf("Charlie cannot buy coffee.\n");
79         }
80     }
81     return 0;
82 }

poj1787Charlie's Change(多重背包+记录路径+好题)

时间: 2024-12-17 20:04:46

poj1787Charlie's Change(多重背包+记录路径+好题)的相关文章

(多重背包+记录路径)Charlie&#39;s Change (poj 1787)

http://poj.org/problem?id=1787 描述 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

台州 OJ 2537 Charlie&#39;s Change 多重背包 二进制优化 路径记录

描述 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

Charlie&#39;s Change POJ - 1787 (完全背包+记录路径)

完全背包输出路径:对于每一次更新记录一下路径:注意钱币个数: dp[i][0]代表在空间为i时需要多少枚钱币 dp[i][1]用来记录路径 cheek[j]用来记录在j时用了多少i枚钱币 思路在代码中: #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define mem(a,b) memset(a,b,sizeof

POJ - 1787 (多重背包还原路径|| 完全背包)

POJ  1787  Charlie's Change 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

POJ 1293 Duty Free Shop(背包记录路径)

Description Pedro travelled to Europe to take part in the International Olympiad in Informatics and is coming back home. Since all his friends asked him to bring them some gift, he bought two big bags of chocolates (one of Mindt and one of Lilka). Ea

poj1417 带权并查集 + 背包 + 记录路径

True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2713   Accepted: 868 Description After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ashore on a foggy island. Though he was exha

poj 1787 背包+记录路径

http://poj.org/problem?id=1787 Charlie's Change Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4512   Accepted: 1425 Description Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at co

UVA 624(01背包记录路径)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=565 记录路径可以用一个二维数组,记录改变时的量.然后从后往前可以推得所有的值. #include <iostream> #include <string> #include <cstring> #include <cstdlib> #incl

UVA624(01背包记录路径)

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=565 01背包打印路径. #include <cstdio> #include <cstring> using namespace std; const int MAXN=10005; int n,W; int w[30]; int dp[MAXN]; in