zoukankan      html  css  js  c++  java
  • POJ-1787 Charlie's Change (完全背包+输出方案)

    Charlie's Change
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 4505   Accepted: 1420
    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 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.







    以前是dp的在重量一定的情况下的最大重量,而这次是在重量一定的情况下dp时使用的硬币的数量,本职是一样的,类比一下就行了,





     1 #include <stdio.h>
     2 #include <string.h>
     3 using namespace std;
     4 int dp[10005], used[10005], pre[10005], coin[4] = {1, 5, 10, 25}, num[4], ans[26];
     5 int main(){
     6     int p;
     7     while(scanf("%d %d %d %d %d", &p, &num[0], &num[1], &num[2], &num[3]) != EOF){
     8         if(p + num[0] + num[1] + num[2] + num[3] == 0) return 0;
     9         for(int i = 0; i <= p; ++i){
    10             dp[i] = -1e9;
    11         }
    12         memset(pre, 0, sizeof(pre));
    13         pre[0] = -1;
    14         dp[0] = 0;
    15         for(int i = 0; i < 4; ++i){
    16             memset(used, 0, sizeof(used));
    17             for(int j = coin[i]; j <= p; ++j){
    18                 if(dp[j] < dp[j - coin[i]] + 1 && used[j - coin[i]] < num[i]){
    19                     used[j] = used[j - coin[i]] + 1;
    20                     dp[j] = dp[j - coin[i]] + 1;
    21                     pre[j] = j - coin[i];
    22                 }
    23             }
    24         }
    25         if(dp[p] <= 0){
    26             printf("Charlie cannot buy coffee.
    ");
    27             continue;
    28         }
    29         memset(ans, 0, sizeof(ans));
    30         while(1){
    31             if(pre[p] == -1) break;
    32             ans[p - pre[p]]++;
    33             p = pre[p];
    34         }
    35         printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.
    ", ans[1], ans[5], ans[10], ans[25]);
    36     }
    37 }
  • 相关阅读:
    静态邻接表dijkstra
    最短路径系列【最短路径、哈密顿路等】
    python 给多人发送邮件,且将结果添加为附件
    Excel调换数据位置
    try ... except...,好处是执行失败后,仍然可以继续运行
    制作表头,2种方式
    工资表变工资条,2种方式
    C言语教程第一章: C言语概论 (4)
    从红旗5.0说起——看Linux的内存解决
    红旗Linux桌面4.1文本安装历程图解(二)
  • 原文地址:https://www.cnblogs.com/zhangbuang/p/10903975.html
Copyright © 2011-2022 走看看