poj1787--Backpack DP (Specific status + backtracking)--charlie ' s change

Source: Internet
Author: User
Tags integer numbers

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.
The main idea: give you some money, as well as the number of 1.5.10.25 change, ask if you can open, and find the most divided into the situation of small change
The first time to find a complete backpack can also solve this limited problem, always thought can only in the multi-pack, the original with an if condition to determine whether the current num is more than t[i] on the line
1. Complete Backpack Procedure
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;intv[5] = {0,1,5,Ten, -};intdp[10010],ans[10010],num[10010],path[10010],t[5];intp;intMain () { while(~SCANF ("%d",&p)) {         for(inti =1; I <=4; i++) scanf ("%d",&T[i]); if((p+t[1]+t[2]+t[3]+t[4]) ==0) 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++) {memset (num,0,sizeof(num));  for(intj = V[i]; J <= P; J + +){                if(Dp[j-v[i] && Dp[j-v[i]] +1> Dp[j] && num[j-v[i]] <T[i]) {Dp[j]= Dp[j-v[i]] +1;//so that each subsequent state is obtained from the previous one, and satisfies two conditions 1: The number used to go to one is more than useless 2: the number of coins used does not exceed the coin itselfNUM[J] = Num[j-v[i]] +1; PATH[J]= J-v[i];//It's hard to think. Used to record paths                }            }        }        inti =p; if(dp[p]>0){             while(i!=0) {ans[i-path[i]]++;//I-path[i] = i-(i-v[i]) = V[i]i = path[i];//Path[i] means the amount of money that comes with a coin when I have money .} 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;}
View Code

2.

poj1787--Backpack DP (Specific status + backtracking)--charlie ' s change

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.