Making Change

Problem D : Making Change

From:UVA, 166

Submit (Out Of Contest)

Time Limit: 3000 MS

Given an amount of money and unlimited (almost) numbers of coins (we will ignore notes for this problem) we know that an amount of money may be made up in a variety of ways. A more interesting problem arises when goods are bought and need to be paid for, with the possibility that change may need to be given. Given the finite resources of most wallets nowadays, we are constrained in the number of ways in which we can make up an amount to pay for our purchases--assuming that we can make up the amount in the first place, but that is another story.

The problem we will be concerned with will be to minimise the number of coins that change hands at such a transaction, given that the shopkeeper has an adequate supply of all coins. (The set of New Zealand coins comprises 5c, 10c, 20c, 50c, $1 and $2.) Thus if we need to pay 55c, and we do not hold a 50c coin, we could pay this as 2*20c + 10c + 5c to make a total of 4 coins. If we tender $1 we will receive 45c in change which also involves 4 coins, but if we tender $1.05 ($1 + 5c), we get 50c change and the total number of coins that changes hands is only 3.

Write a program that will read in the resources available to you and the amount of the purchase and will determine the minimum number of coins that change hands.

Input

Input will consist of a series of lines, each line defining a different situation. Each line will consist of 6 integers representing the numbers of coins available to you in the order given above, followed by a real number representing the value of the transaction, which will always be less than $5.00. The file will be terminated by six zeroes (0 0 0 0 0 0). The total value of the coins will always be sufficient to make up the amount and the amount will always be achievable, that is it will always be a multiple of 5c.

Output

Output will consist of a series of lines, one for each situation defined in the input. Each line will consist of the minimum number of coins that change hands right justified in a field 3 characters wide.

Sample input

2 4 2 2 1 0  0.95
2 4 2 0 1 0  0.55
0 0 0 0 0 0

Sample output

  2
  3
此题的题意为:给定各种面值钱和钱的个数,去买制定价格的商品,求经手的硬币个数最少为多少?因为商店的钱的数量可以认为是无限的,因此,可以求出各种价值的最小硬币数,对于dp[j]=min(dp[j],dp[j-coin[i]+1]),注意此处dp的初始化应为无穷大。对于,顾客来讲,因为钱的个数是固定的,因此要考虑,在这一条件下,硬币最少化的问题,设硬币的个数为k,则对于dp[j+k*coin[i]]=max(dp[j+k*coin[i]],dp[j]+k);因为数量有限,要从后向前扫,消除前面的影响。

#include<iostream>
#include<iomanip>
#define maxn 110
#define maxsize 10000
using namespace std;
 const int coin[6]={1,2,4,10,20,40};
 int num[6];
 int dp[maxn],dd[2*maxn];
 int min(int a,int b){
    return a<b?a:b;
 }
 void fact(){
     dp[0]=0;
     for(int i=1;i<maxn;i++) dp[i]=maxsize;
    for(int i=0;i<6;i++){
        for(int j=coin[i];j<maxn;j++){
            dp[j]=min(dp[j-coin[i]]+1,dp[j]);
        }
    }
 }

 int main(){
     double m;
     int n;
     fact();
     while(cin>>num[0]>>num[1]>>num[2]>>num[3]>>num[4]>>num[5]){
            if(!(num[0]||num[1]||num[2]||num[3]||num[4]||num[5])) break;
            dd[0]=0;
            for(int i=1;i<2*maxn;i++) dd[i]=maxsize;
            cin>>m;
            int mm=int(m*20);
            for(int i=0;i<6;i++){
                for(int j=mm+maxn-1;j>=0;j--){
                    for(int k=1;k<=num[i];k++){
                        if(j+k*coin[i]<2*maxn){
                            dd[j+k*coin[i]]=min(dd[j+k*coin[i]],dd[j]+k);
                        }
                    }
                }
            }
            int minnum=maxn;
            for(int i=0;i<maxn;i++){
                minnum=min(minnum,dp[i]+dd[i+mm]);
            }
            cout<<setw(3)<<minnum<<endl;
     }
    return 0;
 }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值