poj 1787 Charlie's Change(题解)

解决Charlie如何使用有限的硬币组合精确支付咖啡价格的问题,利用动态规划算法进行优化选择,确保使用最多的硬币数。

题目直通车
harlie 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.

题解:看到这题,很明显,多重背包,困难的在于要记录每张钱的个数,我也是看了很多博客,我觉得最好理解的就是用一个二维数组储存在该状态下某张钱使用的次数,下面代码中附了详细讲解

#include<iostream>
#include<math.h>
#include<stdio.h>
#include<string.h>
using namespace std;
#define inff 0x3fffffff

int a[5];
int p;
int dp[10][10000+20];
int use[10][10000+20];
int main()
{
    while(cin>>p>>a[1]>>a[2]>>a[3]>>a[4])
    {if(p+a[1]+a[2]+a[3]+a[4]==0)
       break;
       for(int i=0;i<=4;i++)//一定要从0开始
         {for(int j=0;j<=p;j++)
           dp[i][j]=-inff;
         }


      int money[5];
      money[1]=1;
      money[2]=5;
      money[3]=10;
      money[4]=25;
        memset(use,0,sizeof(use));
      dp[0][0]=0;
     for(int i=1;i<=4;i++)
     { for(int j=0;j<=p;j++)
            {dp[i][j]=dp[i-1][j];//如果上面初始化不从0开始,一些本应该是负无穷的量就从0转移过来而变成了0
            if(money[i]>j)
                continue;
             if(dp[i][j]<dp[i-1][j-money[i]]+1&&a[i]>=1)//dp[i-1][j-qian[i]]+1 这里考虑对于j面额,第一次使用money[i]的情况,因为是第一次使用状态要从dp[I-1]转移过来;
                    {
                        dp[i][j]=dp[i-1][j-money[i]]+1;//因为dp[i][j]<dp[i-1][j-money[i]]+1  说明使用money[1]后要的钱的张数变多了
                        use[i][j]=1;//表示money[i]在表示j面额时使用的张数
                    }

            if(dp[i][j]<dp[i][j-money[i]]+1&&use[i][j-money[i]]+1<=a[i])//这里考虑的是不是第一次使用,use[i][j-money[i]]+1<=c[i]因为use记录使用的张数,则使用的张数要小于总的数量
                    {
                        dp[i][j]=dp[i][j-money[i]]+1;
                        use[i][j]=use[i][j-money[i]]+1;
                    }


            }



     }

      if(dp[4][p]<0)
        {

            cout<<"Charlie cannot buy coffee."<<endl;
            continue;
        }//无法表示
        int num[10];//存每张钱使用的数量;
        for(int i=4;i>=1;i--)
        {num[i]=use[i][p];//对于这个状态使用money[i]的个数
         p-=money[i]*num[i];


        }
         printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",num[1],num[2],num[3],num[4]);


    }
}

如有疑问请下方留言,大家一起交流,一起进步

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值