Charlie's Change(POJ-1787)
来源:互联网 发布:mac把原唱边伴奏 编辑:程序博客网 时间:2024/06/03 14:48
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.
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分的硬币,怎么用最多的硬币买m分的咖啡;
ps:记录方案很好;
#include<iostream>#include<cstring>using namespace std;int m;int f[]={1,5,10,25};int a[4];int p[10005];int ans[10005];int dp[10005];int b[10005];int main(){while(cin>>m>>a[0]>>a[1]>>a[2]>>a[3]){if(m==0&&a[0]==0&&a[1]==0&&a[2]==0&&a[3]==0) break;memset(dp,-1,sizeof(dp));dp[0]=0;memset(p,0,sizeof(p));for(int i=0;i<4;i++){ //第几个memset(ans,0,sizeof(ans)); //每次都初始化一下数量for(int j=f[i];j<=m;j++){ //背包容量,正序,但是有限制if(dp[j]<dp[j-f[i]]+1&&dp[j-f[i]]>=0&&ans[j-f[i]]<a[i]){ //dp[j-f[i]]>=0是看能不能有j大小的包,ans[j-f[i]]<a[i]是用来限制数量的;dp[j]=dp[j-f[i]]+1;ans[j]=ans[j-f[i]]+1; //数量记录p[j]=j-f[i]; //记录路径}}}memset(b,0,sizeof(b));if(dp[m]<0) cout<<"Charlie cannot buy coffee."<<endl;else{int i=m;while(1){ //读取路径if(i==0) break;b[i-p[i]]++;i=p[i];}cout<<"Throw in "<<b[1]<<" cents, "<<b[5]<<" nickels, "<<b[10]<<" dimes, and "<<b[25]<<" quarters."<<endl;}}return 0;}
阅读全文
0 0
- Charlie's Change(POJ-1787)
- POJ 1787 Charlie's Change
- POJ 1787 Charlie's Change
- poj 1787 Charlie's Change
- poj 1787 Charlie's Change
- POJ 1787 Charlie's Change
- poj 1787 Charlie's Change
- Charlie's Change POJ
- POJ 1787 Charlie's Change(多重背包+记录路径)
- poj 1787 Charlie's Change(背包问题输出路径)
- 【POJ】1787 Charlie's Change(完全背包)
- poj 1787 Charlie's Change 题解(动态规划)
- POJ 题目1787 Charlie's Change(完全背包)
- POJ 1787 Charlie's Change 背包问题
- POJ 1787 Charlie's Change 背包问题
- POJ 1787 Charlie's Change / 完全背包
- POJ--1787--Charlie's Change--背包变形
- POJ 1787 Charlie's Change 最多金币
- C++搜索与回溯算法之素数环问题
- Json-c编写一个简单的 Json parser
- python 爬虫
- 0010_Regular Expression Matching
- ubuntu安装 配置tomcat
- Charlie's Change(POJ-1787)
- Java中controller传值乱码
- 由图片的URL 得到图片
- 元胞自动机
- 计算机体系结构-摘要
- leetcode 39. Combination Sum
- EOJ 3260 大学生程序设计邀请赛(华东师范大学)-C 袋鼠妈妈找孩子
- Ubuntu 14.04下CUDA8.0 + cuDNN v5 安装配置过程
- 用cnavas绘制五角星