UVA 166 Making Change

Source: Internet
Author: User
Tags min

Original question:
166 Making Change
Given An amount of money and unlimited (almost) numbers of coins we know the an amount of the money may is made up in a varie Ty of ways. A more interesting problem arises when goods is bought and need to being paid for, with the possibility this change may need to B e given. Given the finite resources of the most wallets nowadays, we is constrained in the number of ways in which we can do up an A Mount to pay for our purchases--assuming that we can do up the amount in the first place, but that's the another story.
The problem we'll be concerned with'll be to minimise the number of coins that change hands
At such a transaction, given the shopkeeper have an adequate supply of all coins. (The set of New Zealand coins comprises 5c, 10c, 20c, 50c,¥1 and¥2.) Th us if w e need to pay 55c, and we don't hold a 50c coin, we could pay this as 2*20c + 10c + 5c to make a total of 4 co Ins. If we tender¥1 W e would receive 45c in which also involv es 4 coins, but if we tender¥1.05 (¥1 + 5c), we get 50 C Change and the total number of coins that changes hands are only 3.
Write a program that would read in the resources a vailable to you and the amount of the purchase and would determine the MI Nim um n um b er of coins that C hange hands.
Input
Input would consist of a series of lines, each line defining a diffierent situation. Each line would consist of 6 in tegers representing the numbers of coins a vailable the order given above, follow Ed by a real number representing the value of the transaction, which'll always is less than¥5.00. The file is terminated by six zero as (0 0 0 0 0 0). The total value of the coins would alw ays be sufficient to mak e up the amount and the amount would always be achievable, t Hat is it would always be a multiple of 5c.
Out put
Output would consist of a series of lines, one for each situation defined in the input. Each line would 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 out put
2
3
English:
Looking for change problem, now you have 5c, 10c, 20c, 50c, ¥ 1, ¥2 coins (1¥=100c), give you the number of coins in your wallet, and then give you a number of not more than 5¥ to indicate the amount of money you want to pay, ask you in the process of trading the number of coins used in the minimum is how many. Here, for example, you want to trade the amount is 15c but you do not have 10c and 5c, you can give the salesperson a 20c, and then the salesperson to find you 5c, then this total use of 2 coins. (Suppose the salesman has countless coins in his hand)

#include <bits/stdc++.h> using namespace std;
const int inf=999999;
int money[6]={1,2,4,10,20,40};
int num[6];
int change[3001];
int give[3001];
Double N;
    int main () {Ios::sync_with_stdio (false);
    memset (change,0,sizeof (change));
        for (int j=1;j<3001;j++) {int tmp=j,cnt=0;
                for (int i=5;i>=0;i--) {if (Tmp>=money[i]) {cnt+= (tmp/money[i]);
                tmp-= (money[i]* (tmp/money[i));
            if (tmp==0) break;
    }} change[j]=cnt;
        } while (Cin>>num[0]>>num[1]>>num[2]>>num[3]>>num[4]>>num[5]>>n) {
        if (num[0]+num[1]+num[2]+num[3]+num[4]+num[5]==0) return 0;
        for (int i=0;i<3001;i++) Give[i]=inf;
        give[0]=0;
        int x= (n+0.005) *100.0;
x/=5;
        cout<<x<<endl;
        int tot=0;
         for (int i=0;i<6;i++)   Tot+=num[i]*money[i];
        cout<<tot<< "" <<x<<endl; for (int i=0;i<6;i++) {for (int. j=1;j<=num[i];j++) {for (int k=tot;k&
                gt;=money[i]*j;k--) {give[k]=min (give[k],give[k-money[i]*j]+j);
        }}} int ans=give[x];
        for (int i=x+1;i<=100;i++) {ans=min (ans,give[i]+change[i-x]);
    } COUT&LT;&LT;SETW (3) <<ans<<endl;
} return 0;
 }

Answer:
More real change money problem, hehe. First of all, then the 5c, 10c, 20c, 50c, ¥ 1, ¥2 into 5,10,20,50,100,200, and then the input payment number into an integer, here pay attention to the conversion accuracy is lost, need to add 0.005 multiplied by 100.0. Since it tells you that all the amounts are multiples of 5, you can use a simple small discretization technique to divide the values by 5. Then the amount of the coin can become 1,2,4,10,20,40, and the number of payments divided by 5.
First think about the original coin-changing problem, give you a value, ask you how many kinds of ways to change coins. The equivalent is a complete backpack, the value of the coin is the volume, the number of coins is worth. Obviously, the value of each coin is 1. The
now gives you the number of each coin, so you can consider a multiple knapsack problem. If you simply consider using some of your own money to collect payments, then according to the multi-backpack solution to the minimum number of coins can be written to write such a transfer equation
Dp[j]=min (number of dp[j],dp[j-money[i]xk]+k (k≤money[i)
However, there may be a few coins in hand, such as to pay 15, your hand 5 and 10 of the coins are 0, but you have a 20 coins, then you can give the salesperson 20c and then the salesperson to find you 5c.
This situation does not satisfy the above transfer equation, because to calculate 15 of the minimum amount of money, set dp[15], the transfer equation can only find the previous state, that is, can only find less than 15 of the state, can not find more than 15.
There are three workarounds for this scenario
1. Add a latitude to describe the current state, such as dp[15][i] you can find Dp[20][i-k]
2. Find equivalent substitution, or change the state value , for example, to give more money to the salesman, and then let the salesman change, in fact, the salesperson received your money after the money should be looking for you to use the least way to find you a coin
3. Memory search, push forward from behind
The first method to draw a two-dimensional table, found not good to make the
second method, consider the payment of 55c of money, the first can make their own, you can also give the extra 55, let the salesman change money. Then, you can transfer to 55 of the state is from 55 to the total amount of money you have, because, you can put your body of money to the salesman, and then let the salesman change money, of course, the result than must be optimal.

So you can do this by first calculating all the money in the body, and then using the multi-backpack method to calculate all the values of Give[tot] to give[55]. Show 55 to Tot, only with the amount of money on their own to give the minimum number, can not give the INF value to put in. First with his own body of change 55, if can gather out, find the smallest value can be together, save to ans, if not, then enumeration is greater than 55 value I, to the salesperson's money at least with how many coins, with give said, Then use I-55 to indicate the minimum number of coins the salesperson is looking for for your money. The give and the change and represents the minimum number of coins for this transaction.
then Ans=min (ans,give+change).
You can make a list of the salesman's change, because the salesperson's various coins have unlimited number, greedy method can solve the salesperson's minimum value of change.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.