ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

POJ-1787 Charlie's Change (完全背包+输出方案)

2019-05-22 09:52:00  阅读:281  来源: 互联网

标签:program coffee Charlie coins 1787 num POJ should


Charlie's Change
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 4505   Accepted: 1420
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.

 







 

以前是dp的在重量一定的情况下的最大重量,而这次是在重量一定的情况下dp时使用的硬币的数量,本职是一样的,类比一下就行了,





 1 #include <stdio.h>
 2 #include <string.h>
 3 using namespace std;
 4 int dp[10005], used[10005], pre[10005], coin[4] = {1, 5, 10, 25}, num[4], ans[26];
 5 int main(){
 6     int p;
 7     while(scanf("%d %d %d %d %d", &p, &num[0], &num[1], &num[2], &num[3]) != EOF){
 8         if(p + num[0] + num[1] + num[2] + num[3] == 0) return 0;
 9         for(int i = 0; i <= p; ++i){
10             dp[i] = -1e9;
11         }
12         memset(pre, 0, sizeof(pre));
13         pre[0] = -1;
14         dp[0] = 0;
15         for(int i = 0; i < 4; ++i){
16             memset(used, 0, sizeof(used));
17             for(int j = coin[i]; j <= p; ++j){
18                 if(dp[j] < dp[j - coin[i]] + 1 && used[j - coin[i]] < num[i]){
19                     used[j] = used[j - coin[i]] + 1;
20                     dp[j] = dp[j - coin[i]] + 1;
21                     pre[j] = j - coin[i];
22                 }
23             }
24         }
25         if(dp[p] <= 0){
26             printf("Charlie cannot buy coffee.\n");
27             continue;
28         }
29         memset(ans, 0, sizeof(ans));
30         while(1){
31             if(pre[p] == -1) break;
32             ans[p - pre[p]]++;
33             p = pre[p];
34         }
35         printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n", ans[1], ans[5], ans[10], ans[25]);
36     }
37 }

 

标签:program,coffee,Charlie,coins,1787,num,POJ,should
来源: https://www.cnblogs.com/zhangbuang/p/10903975.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有