min- imum number of coins required to pay

8 vues (au cours des 30 derniers jours)
max finer
max finer le 7 Déc 2020
Commenté : Stephan le 7 Déc 2020
Suppose that you are working as a cashier in a supermarket and there are
no banknotes left in the cash box. Therefore, for each transaction, you must pay the
remainder of the money by coins.
Write a function numberOfCoins(totalAmount, paidAmount) that returns the min-
imum number of coins required to pay the remainder of the money to the customer.
If the customer pays less than the total amount of transaction, the function should
give the following error message:
The customer paid less than the total amount of the transaction
and abort. Note that you have sucient amounts of 1kr, 5kr, 10kr, 25kr, 50kr, 1TL
coins in the cash box.
The following table shows a possible list of outputs of the function numberOfCoins.
Function call Output
numberOfCoins(99.12, 100) 6
numberOfCoins(17.95, 20) 3
numberOfCoins(10, 10) 0
numberOfCoins(3.99, 3.5) Error
Table 1: A list of possible outputs of the function numberOfCoins
For instance, if the total transaction is 99.12TL and the customer gives you 100TL,
you must pay 1x50kr, 1x25kr, 1x10 kr and 3x1kr coins to the customer (6 coins in
total).
I tried like this but i couldnt:
function coins=numberOfCoins(totalAmount, paidAmount)
totalAmount=input('totalAmount: ');
paidAmount=input('paidAmount: ');
if paidAmount>=totalAmount
transaction = paidAmount - totalAmount;
while transaction >= 1
transaction = transaction - 1.00;
numberOfCoins = numberOfCoins + 1;
end
if transaction >= 0.50
transaction = transaction - 0.50;
numberOfCoins = numberOfCoins + 1;
elseif transaction >= 0.25
transaction = transaction - 0.25;
numberOfCoins = numberOfCoins + 1;
elseif transaction >= 0.10
transaction = transaction - 0.10;
numberOfCoins = numberOfCoins + 1;
elseif transaction >= 0.05
transaction = transaction - 0.05;
numberOfCoins = numberOfCoins + 1 ;
elseif transaction >= 0.01
transaction = transaction - 0.01;
numberOfCoins = numberOfCoins + 1;
end
end
end

Réponse acceptée

Stephan
Stephan le 7 Déc 2020
You have some issues in your if-else structure, but also you have a numerical problem - use uint datatype to avoid this:
[NumCoins1, UniqueCoinNumber1, CoinsCollection1] = numberOfCoins(99.12, 100)
[NumCoins2, UniqueCoinNumber2, CoinsCollection2] = numberOfCoins(17.95, 20)
[NumCoins3, UniqueCoinNumber3, CoinsCollection3] = numberOfCoins(10, 10)
[NumCoins4, UniqueCoinNumber4, CoinsCollection4] = numberOfCoins(3.99, 3.5)
function [NumCoins, UniqueCoinNumber, CoinsCollection] = numberOfCoins(totalAmount, paidAmount)
% totalAmount=input('totalAmount: ');
% paidAmount=input('paidAmount: ');
coins = [];
if paidAmount<totalAmount
disp('The paid amount is less then the total amount')
NumCoins = NaN;
UniqueCoinNumber = NaN;
CoinsCollection = NaN;
else
transaction = paidAmount - totalAmount;
transaction = uint16(transaction * 100);
while transaction > 0
if transaction >= 100
transaction = transaction - 100;
coins = [coins 1];
elseif transaction >= 50
transaction = transaction - 50;
coins = [coins 0.5];
elseif transaction >= 25
transaction = transaction - 25;
coins = [coins 0.25];
elseif transaction >= 10
transaction = transaction - 10;
coins = [coins 0.1];
elseif transaction >= 5
transaction = transaction - 5;
coins = [coins 0.05];
elseif transaction >= 1
transaction = transaction - 1;
coins = [coins 0.01];
end
end
NumCoins = numel(coins);
UniqueCoinNumber = numel(unique(coins));
CoinsCollection = coins;
end
end
  3 commentaires
John D'Errico
John D'Errico le 7 Déc 2020
Hi Stephan. Thank you for the kind comment about my answer.
Stephan
Stephan le 7 Déc 2020
Gladly, it's actually just the truth

Connectez-vous pour commenter.

Plus de réponses (1)

John D'Errico
John D'Errico le 7 Déc 2020
Well, you did make a credible effort. More than most students seem to post. Sigh. :)
First, you say that the code you wrote does not work. I can see at least 2 reasons why it will fail. But why do YOU claim it does not work? When something fails, explain what failed! If you want help, you need to help us to know what you think is wrong.
What do I see? Do NOT use decimal coin amounts!!!!!!!!!!!!!!!!!!!!!! Do NOT use decimals. Do NOT use decimals. There, I said it three times, so it must be true.
But why did I say that? The problem is, you are testing for essential equalities with decimals. And a double precision number CANNOT represent decimal fractions exactly, just as you cannot represent the number 1/3 as a decimal, in a finite number of digits. The same applies to the numbers 0.10 or 0.05. So if we try to represent 0.05 in binary, we would get an infinitely repeating bit sequence like this:
'0.000011001100110011001100110011001100110011001100110011010...'
We see that in terms of how MATLAB stores the number 0.05.
sprintf('%0.55f',0.05)
ans = '0.0500000000000000027755575615628913510590791702270507812'
And that causes fallacies like this to happen:
0.05*3 == 0.15
ans = logical
0
Instead, multiply by 100. Now everything is an integer. All of your mathematics will now work happily.
5*3 == 15
ans = logical
1
The kr would indicate we are probably talking about integer multples of a krone, but what matters is you need to work in terms of integer multiples of the common denominator of your coin set. So here that would be [1,5,10,25,50,100].
The next question is, if the greedy algorithm you implemented will always result in the optimsl set. A greedy algorithm here tries to make change by always using the largest coin denomination first. That would fail, for example, if your coin set was the set:
[1,5,10,20,25,50,100].
Now, what is the minimal set needed to make change where 40 kr is desired? The greedy algorithm would see that 25kr is possible, so your greedy algorithm will choose the set [25,10,5], for 3 coins. However, it could also find [20,20], which requires only two coins.
As it turns out however, I think the greedy algorithm works here. Though perhaps I should say it survives, since I cannot see an immediate counter-example for the coin set you will be using.
Next, I think you do not understand functions. You do this:
function coins=numberOfCoins(totalAmount, paidAmount)
totalAmount=input('totalAmount: ');
paidAmount=input('paidAmount: ');
so while you pass in the variables paidAmount and totalAmount into the function, then you use input to overwrite those numbers.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by