# Thread: Alogrithm help - bank notes

1. ## Alogrithm help

(We have 1000, 500, 200, 100, 50, 20, 10, 5, 2, 1 bank notes available, also we should be able to change the notes available).

2. I smell assignment.

What do you have so far?

3. PHP Code:
``` \$notes_array = array(1000, 500, 200, 100, 50, 20, 10, 5, 2, 1 );\$amount = 1520;foreach(\$notes_array as \$note_type) {    echo \$note_type."<br />";    \$notes[\$note_type] = (int) (\$amount/\$note_type);    \$amount = \$amount - \$note[\$note_type]."<br />"  ;    //echo \$notes[\$note_type]."<br />"  ;}  ```

4. Subtract as many 1000s from your value as you can without going below 0, then as many 500s, then as many 200s, etc. until you reach exactly 0. Keep track of how many of each note you subtracted to get there.

5. Originally Posted by eanimator
didnt get it
PHP Code:
``` \$notes_array = array(1000, 500, 200, 100, 50, 20, 10, 5, 2, 1 );\$amount = 1520;\$left = \$amount;\$curnote = 0;\$notes_used = array();for (\$i = 0; \$i < count(\$notes_array); \$i++) \$notes_used[\$i] = 0;while (\$left > 0) {    if (\$left - \$notes_array[\$curnote] >= 0) {      \$left = \$left - \$notes_array[\$curnote];      \$notes_used[\$curnote]++;    } else {      \$curnote++;    }}foreach (\$notes_array as \$key => \$val) {  echo \$val . " notes used: " . (isset(\$notes_used[\$key]) ? \$notes_used[\$key] : 0) . "<br />\n";}  ```
1000 notes used: 1
500 notes used: 1
200 notes used: 0
100 notes used: 0
50 notes used: 0
20 notes used: 1
10 notes used: 0
5 notes used: 0
2 notes used: 0
1 notes used: 0

6. I got this...

PHP Code:
``` <?php\$iTargetAmount = 1250;while(\$iCurrentChange != \$iTargetAmount){    foreach(array(1000, 500, 200, 100, 50, 20, 10, 5, 2, 1) as \$iCoin)    {        if((\$iCurrentChange + \$iCoin) <= \$iTargetAmount)        {            \$iCurrentChange += \$iCoin;            \$aCoins[] = \$iCoin;            break;        }    }}printf(    'We made %s using the following %s coin(s): %s',    \$iTargetAmount,    count(\$aCoins),    implode(', ', \$aCoins)); #We made 1250 using the following 3 coin(s): 1000, 200, 50?> ```
This seems more logical to me, I think this is how a 'person' would work it out.

7. Originally Posted by eanimator
Thanksssssssss
At least make sure you understand it before turning it in as your solution

8. Chances are you're going to be asked about how it works, so you need to be prepared for that.

So, EAnimator, what is the code you're going to be using and how does it work?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•