Poj1787charlie's change

Source: Internet
Author: User
Tags integer numbers
Charlie's change
Time limit:1000 ms   Memory limit:30000 K
Total submissions:2978   Accepted:844

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 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

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 shoshould be generated for it.

Output

For each situation, your program shocould 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 shoshould 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

A. Given a total amount, then the number of each coin is, and 25 respectively, ask how many coins can be paid exactly
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <queue> const int MX = 10000; using namespace STD; int main () {int I, J, K, G [4], V, C [4] = {11000,}, DP [] [5]; // DP [.] [0] indicates the largest total coin, DP [.] [1 ~ 4] represents the number of each coin respectively while (1) {int S = 0; scanf ("% d", & V); S + = V; for (I = 0; I <4; I ++) {scanf ("% d", G + I); S + = G [I] * C [I];} // cout <S <Endl; If (S = 0) break; s-= V; If (S <v) {printf ("Charlie cannot buy coffee. \ n ");} else if (S = V) {printf (" Throw in % d cents, % d nickels, % d dimes, and % d quarters. \ n ", G [0], G [1], G [2], G [3]);} else {memset (DP,-MX, sizeof DP ); for (I = 0; I <5; I ++) DP [0] [I] = 0; for (I = 0; I <4; I ++) {k = 1; while (G [I]-k> = 0) {for (j = V; j> = C [I] * k; j --) {If (DP [J] [0] <DP [J-C [I] * k] [0] + k) {DP [J] [0] = DP [J-C [I] * k] [0] + k; For (INT x = 1; x <5; X ++) DP [J] [x] = DP [J-C [I] * k] [x]; DP [J] [I + 1] + = K;} G [I]-= K; k <= 1 ;}if (G [I]> 0) {k = G [I]; for (j = V; j> = C [I] * k; j --) {If (DP [J] [0] <DP [J-C [I] * k] [0] + k) {DP [J] [0] = DP [J-C [I] * k] [0] + k; For (INT x = 1; x <5; X ++) DP [J] [x] = DP [J-C [I] * k] [x]; DP [J] [I + 1] + = K ;}}} for (I = 0; I <5; I ++) {If (DP [v] [I] <0) {printf ("Charlie cannot buy coffee. \ n "); break ;}}if (I = 5) {printf (" Throw in % d cents, % d nickels, % d dimes, and % d quarters. \ n ", DP [v] [1], DP [v] [2], DP [v] [3], DP [v] [4]) ;}} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.