zoukankan      html  css  js  c++  java
  • Charlie's Change_完全背包&&路径记录

    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.



    【题意】给出p,c1,c2,c3,c4,p是总金额,c1,c2,c3,c4分别是给出的四种面值的硬币的数量,求合起来是p使用最多硬币的情况下,各种硬币都用了多少。

    【思路】背包。并且要记录各种硬币的数量,用一个数组path[p],path[p]=p-v[i];p-path[p]=v[i];巧用path解决了各种硬币数量的问题。接着背包问题,他是限定数量的,要判断num[j-v[i]]是否小于c[i],还要判定dp[j]是否存在即是否大于0,以及dp[j-v[i]]+1是否大于dp[j].

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    using namespace std;
    const int N=10005;
    int p;
    int v[5]= {0,1,5,10,25};
    int c[5];
    int dp[N],num[N],path[N],ans[N];
    int main()
    {
        while(~scanf("%d%d%d%d%d",&p,&c[1],&c[2],&c[3],&c[4]))
        {
            if(!p&&!c[1]&&!c[2]&&!c[3]&&!c[4]) break;
            memset(dp,0,sizeof(dp));
            memset(ans,0,sizeof(ans));
            memset(path,0,sizeof(path));
            dp[0]=1;
            for(int i=1; i<=4; i++)//背包
            {
                memset(num,0,sizeof(num));//每个i更新,都要初始化
                for(int j=v[i]; j<=p; j++)
                {
                    if(num[j-v[i]]<c[i]&&dp[j-v[i]]&&dp[j-v[i]]+1>dp[j])
                    {
                        dp[j]=dp[j-v[i]]+1;
                        num[j]=num[j-v[i]]+1;
                        path[j]=j-v[i];
                    }
    
                }
            }
            int i=p;
            if(dp[p]>0)//判断是否存在
            {
                while(i!=0)
                {
                    ans[i-path[i]]++;//巧用path,进行路径记录
                    i=path[i];
                }
                printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.
    ",ans[1],ans[5],ans[10],ans[25]);
            }
            else printf("Charlie cannot buy coffee.
    ");
        }
        return 0;
    }
  • 相关阅读:
    基于角色的权限设计(一)
    js图片懒加载插件封装
    项目中必须知道有关css和html的常识
    设为主页代码及添加到收藏夹代码大全
    JS弹出层、弹窗效果+拖曳功能
    算数验证码
    js基础知识
    基于角色的权限设计(二)
    sqlserver数据类型char和nchar,varchar和nvarchar,text和ntext的用法以及区别?
    经典页面布局,任何分辨率下,全屏显示
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5998508.html
Copyright © 2011-2022 走看看