Poj 1787 multi-backpack recording path

Source: Internet
Author: User
Tags integer numbers

The first record path

Charlie's change
Time limit:1000 ms   Memory limit:30000 K
Total submissions:1998   Accepted:476

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.
View code

# Include <stdio. h>
# Include < String . H>
Int V [ 4 ] = { 1 , 5 , 10 , 25 };
Int DP [ 10010 ];
Int P [ 10010 ], Used [ 10010 ];
Int Num [ 4 ], ANS [30 ];
Int Main ()
{
Int M, I, J;
While (Scanf ( " % D " , & M )! = EOF)
{
Int Sum = m;
For (I = 0 ; I < 4 ; I ++)
{
Scanf ( " % D " , & Num [I]);
Sum + = num [I];
}
If (Sum = 0 ) Break ;
Memset (DP, 0 , Sizeof (DP ));
Memset (p, 0 , Sizeof (P ));
P [ 0 ] =- 1 ; DP [ 0 ] = 1 ;
For (I = 0 ; I < 4 ; I ++)
{
Memset (used, 0 , Sizeof (Used ));
For (J = V [I]; j <= m; j ++)
If ((! DP [J] | DP [J-V [I] +1 > DP [J]) & DP [J-V [I] & used [J-V [I] <num [I])
{
DP [J] = DP [J-V [I] + 1 ;
Used [J] = used [J-V [I] + 1 ;
P [J] = J-V [I];
}
}
Memset (ANS, 0 , Sizeof (ANS ));
If (DP [m])
{
I = m;
While ( 1 )
{
If (P [I] =- 1 ) Break ;
Ans [I-P [I] ++;
I = P [I];
}
Printf ( " Throw in % d cents, % d nickels, % d dimes, and % d quarters. \ n " , ANS [ 1 ], ANS [5 ], ANS [ 10 ], ANS [ 25 ]);
}
Else Printf ( " Charlie cannot buy coffee. \ n " );
}
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.