Charlie's change
Time Limit: 2 seconds memory limit: 65536 KB
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 shocould output which coins Charlie has to use paying the coffee so that he uses
As your coins as possible. Because Charlie really does not want any change back he wants to pay the price exactly.
Input Specification
Each line of the input contains five integer numbers separated by a single space describing one situation to solve. The first integer on the lineP, 1P10 000,
Is the coffee price in cents. Next Four integers,C1,C2,C3,C4, 0CI10 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 shoshould be generated for it.
Output Specification
For each situation, your program shocould output one line containing the string"Throw in T1 cents, T2 nickels, T3 dimes, and T4 quarters.", WhereT1,T2,T3,T4 are
The numbers of coins of appropriate values Charlie shocould use to pay the coffee while using as your coins as possible. in the case Charlie does not possess enough change to pay the price of the coffee exactly, your program shocould 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.
Source:Czech Technical University open 2003
Question: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 2156.
Analysis: this is still a problem of payment. The practice is still multiple backpacks. The difference is that the specific installation method of the backpack is required, that is, record the transfer path.
Code:
#include<cstdio>using namespace std;const int mm=11111;int v[]= {1,5,10,25};int f[mm],p[mm],t[mm],c[4],ans[4];int i,j,m;void CompletePack(int v,int k){ for(int i=v;i<=m;++i) if(f[i-v]>=0&&f[i]<=f[i-v]) { f[i]=f[i-v]+1; p[i]=i-v; t[i]=k; }}void ZeroOnePack(int v,int d,int k){ for(int i=m;i>=v;--i) if(f[i-v]>=0&&f[i]<f[i-v]+d) { f[i]=f[i-v]+d; p[i]=i-v; t[i]=k; }}int main(){ while(scanf("%d%d%d%d%d",&m,&c[0],&c[1],&c[2],&c[3]),m+c[0]+c[1]+c[2]+c[3]) { for(i=0; i<=m; ++i)f[i]=-1000000000; f[0]=0; for(i=0; i<4; ++i) if(c[i]) { if(c[i]*v[i]>=m)CompletePack(v[i],i); else { j=1; while(j<c[i]) { ZeroOnePack(v[i]*j,j,i); c[i]-=j; j<<=1; } ZeroOnePack(v[i]*c[i],c[i],i); } } if(f[m]>=0) { for(i=0; i<4; ++i)ans[i]=0; while(m) { ans[t[m]]+=(m-p[m])/v[t[m]]; m=p[m]; } printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",ans[0],ans[1],ans[2],ans[3]); } else puts("Charlie cannot buy coffee."); } return 0;}