(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).
| SitePoint Sponsor |
(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).
Last edited by eanimator; Apr 13, 2009 at 02:32.
I smell assignment.
What do you have so far?
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.
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 />" ;
}


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.
17-29% of paid ad clicks are fraudulent. Get protected with Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more.


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
17-29% of paid ad clicks are fraudulent. Get protected with Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more.
I got this...
This seems more logical to me, I think this is how a 'person' would work it out.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
?>
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


17-29% of paid ad clicks are fraudulent. Get protected with Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more.
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?
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
Bookmarks