Question 1/1 - PHP Here are some examples of how change may be returned: Change for €1 Possible Solutions Impossible €2 + €2+ €2 Optimal Solution Impossible €2+ €2+ €2 €6 €10 €2 + €2 + €2 +€2 + €10 €2 €5 + €5 €10 €9223372036854775807 (€10 * 922337203685477580) + €5 + €2 Change is represented as a change object. This object has three properties: coin2, bil15 and bill10 which represents the numbers of €2 coins, €5 bills and €10 bills. For instance, when applied to example #2 of the table above (€6), we should get a change object with the following values: ● coin2 value is 3 (3 €2 coins) ● bil15 value is 0 (no €5 bill) bill10 value is 0 (no €10 bill) Implement the optimalChange ($s) function which returns a Change object. The sum of coins and bills indicated in the Change object must be $s. If it is not possible to give back change - as in example #1 -, the function must return NULL To get a maximum number of points, your solution should always provide a result - when possible - having the minimal number of bills and coins. Constraints: $s is always an integer greater than 0 and less than or equal to 231 - 1 (2147483647). Console output Elapsed time: 00:58/40:00 Answer Test code $s = 10; // Change this value to perform other tests $m optimalChange ($s); echo "Coin (s) 2€: " . $m->coin2 . "\n"; echo "Bill(s) 5€: " . $m->bil15 . "\n"; echo "Bill(s) 10€: " . $m->bill10 . "\n"; $result = $m->coin2*2 + $m->bil15*5 + $m->bil110*10; echo "\nChange given = " . $result; Test Your Code 1 10 11 12 13 14 15 Submit G Ⓒ
Today, stores are increasingly equipped with automated checkout machines. Most of these machines accept payment only by credit card, despite the fact that a significant portion of consumers continue to pay for goods with cash. One of the problems presented by cash transactions is how to return change: what is the optimal way to give back a certain amount, with the minimum number of coins and bills? It's an issue that each of us encounters on a daily basis and the problem is the same for automated checkout machines. In this exercise, you are asked to try and find an optimal solution for returning change in a very specific case: when the machine contains only €2 coins, €5 bills and €10 bills. To simplify the problem, we imagine that all of these coins and bills are available in unlimited quantities. Here are some examples of how change may be returned: Possible Solutions Change for €1 Optimal Solution Impossible Impossible €6 €2 + €2+ €2 €2 + €2 + €2 €10 €10 €2 + €2 + €2 + €2+ €2 €5 + €5 €10 €9223372036854775807 (€10 * 922337203685477580) + €5 + €2 Change is represented as a change object. This object has three properties: [coin2, bills and bill10 which represents the numbers of €2 coins, €5 bills and €10 bills. For instance, when applied to example #2 of the table above (€6), we should get a change object with the following values: coin2 value is 3 (3 €2 coins)
Question 1/1 - PHP Here are some examples of how change may be returned: Change for €1 Possible Solutions Impossible €2
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am