Description
Charlie is a driver of the Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending Ma Chines at Motorests. Charlie hates change. That is basically the setup of your next task.
Your program would be given numbers and types of coins Charlie have and the coffee price. The coffee vending machines accept coins of values 1, 5, ten, and cents. The program should output which coins Charlie have to use paying the coffee so that he uses as many coins as possible. Because Charlie really does not want a change back he wants to pay the price exactly.
Input
Each line of the input contains five integer numbers separated is a single space describing one situation to solve. The first integer on the "line P", 1 <= P <=, is the "coffee price" in cents. Next four integers, C1, C2, C3, C4, 0 <= Ci <=, is the numbers of cents, nickels (5 cents), dimes (cents) , and quarters (cents) in Charlie ' s valet. The last line of the input contains five zeros and no output should is generated for it.
Output
For each situation, your program should output one line containing the string "Throw in T1 cents, T2 nickels, T3 Dimes, an D T4 Quarters. ", where T1, T2, T3, T4 is 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 isn't possess enough change to pay the price of the coffee exactly, your program should output "Cha Rlie 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.
"Test Instructions" gives p,c1,c2,c3,c4,p is the total amount, c1,c2,c3,c4 respectively is given four denominations of the number of coins, to combine it is p use the most coins in the case, a variety of coins are used.
"Thinking" backpack. And to record the number of various coins, with an array path[p],path[p]=p-v[i];p-path[p]=v[i], skillfully with the path to solve the problem of the number of coins. Then the knapsack problem, he is limited quantity, to determine whether Num[j-v[i]] is less than c[i], also to determine whether the existence of dp[j] is greater than 0, and whether dp[j-v[i]]+1 is greater than dp[j].
#include <iostream>#include<stdio.h>#include<string.h>using namespacestd;Const intn=10005;intp;intv[5]= {0,1,5,Ten, -};intc[5];intDp[n],num[n],path[n],ans[n];intMain () { while(~SCANF ("%d%d%d%d%d", &p,&c[1],&c[2],&c[3],&c[4])) { if(!p&&!c[1]&&!c[2]&&!c[3]&&!c[4]) Break; Memset (DP,0,sizeof(DP)); memset (ans,0,sizeof(ans)); memset (Path,0,sizeof(path)); dp[0]=1; for(intI=1; i<=4; i++)//Backpack {memset (num,0,sizeof(num));//Each I update is initialized for(intJ=v[i]; j<=p; J + +) { if(num[j-v[i]]<c[i]&&dp[j-v[i]]&&dp[j-v[i]]+1>Dp[j]) {Dp[j]=dp[j-v[i]]+1; NUM[J]=num[j-v[i]]+1; PATH[J]=j-V[i]; } } } intI=p; if(dp[p]>0)//Determine if there is a { while(i!=0) {ans[i-path[i]]++and//skillfully using path, to record I=Path[i]; } printf ("Throw in%d cents,%d nickels,%d dimes, and%d quarters.\n", ans[1],ans[5],ans[Ten],ans[ -]); } Elseprintf"Charlie cannot buy coffee.\n"); } return 0;}
Charlie ' s change_ full backpack && path record