Charlie ' s change
Time Limit: 1000MS |
|
Memory Limit: 30000K |
Total Submissions: 3720 |
|
Accepted: 1125 |
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: Give the price of what you want to buy p, there are four coins in denominations of 1,5,10,25, and then give the number of each coin, to find out how many coins to buy this thing
This is a good question, num[i][j] used to record the number of J coins in the I state, Dp[i] used to record the number of coins, the request is the number of coins in the case of the number of each coin
1#include <iostream>2#include <cstring>3#include <algorithm>4#include <cstdio>5 6 using namespacestd;7 Const intMAX =10000+Ten;8 intdp[max],num[max][5],c[5],p;9 intw[5]={1,5,Ten, -};Ten voidZeroonepage (intCostintMountintKind//01 Backpack Pass parameters have the number and type of this kind of coin One { A for(inti = P; I >= cost; i--) - { - if(Dp[i-cost] >-1&& Dp[i] < Dp[i-cost] + mount)//To find the most number of cases, 01 of the number needs to pass the parameter, complete backpack is 1 the { -Dp[i] = Dp[i-cost] +Mount; - for(intj =0; J <4; J + +) -NUM[I][J] = Num[i-cost][j];//change the number of each coin in this state +Num[i][kind] + =Mount; - } + } A } at voidCompletepage (intCostintkind) - { - for(inti = Cost; I <= p; i++) - { - if(Dp[i-cost] >-1&& Dp[i] < Dp[i-cost] +1) - { inDp[i] = Dp[i-cost] +1; - for(intj =0; J <4; J + +) toNUM[I][J] = num[i-Cost ] [j]; +Num[i][kind] + =1; - } the } * } $ voidMultiplepage (intCostintMountintkind)Panax Notoginseng { - if(Cost * Mount >=p) the { + completepage (cost, kind); A return; the } + intK =1; - while(K <mount) $ { $Zeroonepage (k *Cost , K, kind); -Mount = mount-K; -K <<=1; the } - if(Mount >0)WuyiZeroonepage (Mount *Cost , mount, kind); the return ; - } Wu intMain () - { About while(SCANF ("%d", &p)! =EOF) $ { - intsum =p; - for(inti =0; I <4; i++) - { Ascanf"%d", &c[i]); +Sum + =C[i]; the } - if(Sum = =0) $ Break; thememset (NUM,0,sizeof(num)); thememset (dp,-1,sizeof(DP)); thedp[0] =0; the for(inti =0; I <4; i++) - if(C[i]) in multiplepage (w[i],c[i],i); the if(Dp[p] >0) the { Aboutprintf"Throw in%d cents,%d nickels,%d dimes, and%d quarters.\n", num[p][0],num[p][1],num[p][2],num[p][3]); the } the Else the { +printf"Charlie cannot buy coffee.\n"); - } the }Bayi return 0; the}
View Code
Poj1787charlie's Change (multiple backpack + record path + good title)