码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1787 Charlie's Change

时间:2015-04-14 16:13:43      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

Charlie‘s Change

Time Limit: 1000ms
Memory Limit: 30000KB
This problem will be judged on PKU. Original ID: 1787
64-bit integer IO format: %lld      Java class name: Main
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 should output which coins Charlie has to use paying the coffee so that he uses as many 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 should be generated for it.
 

Output

For each situation, your program should 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 should use to pay the coffee while using as many coins as possible. In the case Charlie does not possess enough change to pay the price of the coffee exactly, your program should output "Charlie cannot buy coffee.".
 

Sample Input

12 5 3 1 2
16 0 0 0 1
0 0 0 0 0

Sample Output

Throw in 2 cents, 2 nickels, 0 dimes, and 0 quarters.
Charlie cannot buy coffee.

Source

解题:多重背包!由于完全背包是从前往后更新的,因此不再是01背包那样,每次逆序一次遍历,至多选一个,
 
从前往后走,有可能会选多个,所以要限制个数,只有满足个数在范围内才跟更新
 
技术分享
 1 #include <cstdio>
 2 #include <cstring>
 3 using namespace std;
 4 int val[5] = {1,5,10,25},cnt[5],dp[10010],path[10010],p;
 5 int sel[10010];
 6 int main() {
 7     while(~scanf("%d",&p)) {
 8         bool flag = p;
 9         for(int i = 0; i < 4; ++i) {
10             scanf("%d",cnt+i);
11             flag = flag || cnt[i];
12         }
13         if(!flag) break;
14         memset(dp,-1,sizeof dp);
15         memset(path,0,sizeof path);
16         path[0] = -1;
17         for(int i = dp[0] = 0; i < 4; ++i){
18             memset(sel,0,sizeof sel);
19             for(int j = val[i]; j <= p; ++j)
20                 if(dp[j - val[i]] > -1 && dp[j - val[i]] >= dp[j] && sel[j - val[i]] < cnt[i]){
21                     sel[j] = sel[j - val[i]] + 1;
22                     path[j] = j - val[i];
23                     dp[j] = dp[j-val[i]] + 1;
24                 }
25         }
26         if(dp[p] < 0) printf("Charlie cannot buy coffee.\n");
27         else{
28             int ans[110] = {0};
29             while(~path[p]){
30                 ans[p - path[p]]++;
31                 p = path[p];
32             }
33             printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",ans[val[0]],ans[val[1]],ans[val[2]],ans[val[3]]);
34         }
35     }
36     return 0;
37 }
View Code

 

POJ 1787 Charlie's Change

标签:

原文地址:http://www.cnblogs.com/crackpotisback/p/4425057.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!