http://poj.org/problem?id=1787 DescriptionCharlie is a driver of the Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee Vendin G Machines 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.
inputEach 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.Outputfor 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 is the numbers of coins values appropriate Charlie use to pay the COF Fee 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.
for this argument, some people say is a multi-pack, some people say is a complete backpack, anyway I was when the multi-pack looked, but look at the person wrote the complete backpack is not difficult to understand, quite ingenious
#include <stdio.h>#include<iostream>#include<string.h>#include<algorithm>using namespacestd;Const intinf=0x3f3f3f3f;#defineMet (A, B) (Memset (A,b,sizeof (a)))#defineN 11000#defineINF 0x3f3f3f3fintPre[n], v[5]={0,1,5,Ten, -}, Dp[n];intUsed[n];/**dp[i] represents the most needed coin to make up the I-dollar used[i] a few times for the first-I coins (feeling this is good, can turn multiple backpacks into a full backpack) pre[j] records the state from which J was converted .*/intMain () {intP, num[5]={0}; while(SCANF ("%d%d%d%d%d", &p, &num[1], &num[2], &num[3], &num[4]), p+num[1]+num[2]+num[3]+num[4]) { intI, J, ans[ the]={0}; Met (DP,-1); Met (Pre,-1); dp[0] =0; for(i=1; i<=4; i++) {memset (used,0,sizeof(used)); for(J=v[i]; j<=p; J + +) { if(dp[j-v[i]]+1>DP[J] && dp[j-v[i]]>=0&& used[j-v[i]]<Num[i]) {Dp[j]= dp[j-v[i]]+1; USED[J]= used[j-v[i]]+1; PRE[J]= JV[i]; } } } if(dp[p]<0) printf ("Charlie cannot buy coffee.\n"); Else{met (ans,0); I=p; while(1) { if(pre[i]==-1) Break; Ans[i-pre[i]]++; I=Pre[i]; } printf ("Throw in%d cents,%d nickels,%d dimes, and%d quarters.\n", ans[v[1]], ans[v[2]], ans[v[3]], ans[v[4]]); } } return 0;}
(Multiple backpack + record path) Charlie's change (POJ 1787)