POJ 1787 Charlie's Change (full backpack, recording path)

Source: Internet
Author: User
Tags integer numbers
Charlie's Change
Time Limit:1000 MS   Memory Limit:30000 K
Total Submissions:2215   Accepted:575

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

CTU open2003

Given a number p, four coins worth, and 25 must be combined into p, and the number of coins is the maximum. If no solution is provided, "Charlie cannot buy coffee" is output. ", 1 <= p <= 10 thousand, 1 <= number of coins <= 10 thousand

# Include <stdio. h> # include <iostream> # include <string. h ># include <algorithm> using namespace std; const int INF = 0x3f3f3f; int v [4] = {10010,}; int dp []; int path [10010], used [10010]; int num [4]; int ans [100]; // This value must be greater than 25int main () {// freopen ("in.txt", "r", stdin); // freopen ("out.txt", "w", stdout); int P; while (scanf ("% d", & P) {for (int I = 0; I <4; I ++) scanf ("% d ", & num [I]); if (P = 0 & num [0] = 0 & num [1] = 0 & num [2] = 0 & num [3] = = 0) break; for (int I = 0; I <= P; I ++) dp [I] =-INF; memset (path, 0, sizeof (path )); path [0] =-1; dp [0] = 0; for (int I = 0; I <4; I ++) {memset (used, 0, sizeof (used); for (int 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; path [j] = j-v [I] ;}} memset (ans, 0, sizeof (ans); if (dp [P] <0) {printf ("Charlie cannot buy coffee. \ n "); continue;} else {int I = P; while (1) {if (path [I] =-1) break; ans [I-path [I] ++; I = path [I];} printf ("Throw in % d cents, % d nickels, % d dimes, and % d quarters. \ n ", ans [v [0], ans [v [1], ans [v [2], ans [v [3]);} 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.