博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ-1787 Charlie's Change (完全背包+输出方案)
阅读量:4561 次
发布时间:2019-06-08

本文共 3173 字,大约阅读时间需要 10 分钟。

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 
2 #include
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 }

 

转载于:https://www.cnblogs.com/zhangbuang/p/10903975.html

你可能感兴趣的文章
Memcached 笔记与总结(3)安装 php-memcache(windows 系统下)
查看>>
Android2.2中添加的match_parent和fill_parent没有区别
查看>>
POJ 1251 Jungle Roads (prim)
查看>>
IOS_画图 图片等比压缩 IOS_UIImage
查看>>
刘关张三结义(第七次作业)
查看>>
Redis学习笔记(十一) 命令进阶:Connection(连接)
查看>>
memcached与redis 对比
查看>>
JVM 入门三板斧
查看>>
手机整机方案公司之测试业务流程
查看>>
HeadFIrst Ruby 第二章总结 methods and classes
查看>>
STM32 通用定时器相关寄存器
查看>>
【题解】1621. 未命名
查看>>
字符串加密算法
查看>>
Oracle的实例恢复解析
查看>>
UICollectionView cellForItemAt 不被调用
查看>>
巧用网盘托管私人Git项目
查看>>
python全栈脱产第19天------常用模块---shelve模块、xml模块、configparser模块、hashlib模块...
查看>>
[LeetCode] House Robber
查看>>
virtualbox中kali虚拟机安装增强功能
查看>>
java生成六位验证码
查看>>