SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Addict eanimator's Avatar
    Join Date
    Sep 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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).
    Last edited by eanimator; Apr 13, 2009 at 01:32.

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3

  4. #4
    SitePoint Addict eanimator's Avatar
    Join Date
    Sep 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $notes_array = array(100050020010050201052);

    $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 />"  ;



  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Addict eanimator's Avatar
    Join Date
    Sep 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    didnt get it

  7. #7
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by eanimator View Post
    didnt get it
    PHP Code:
    $notes_array = array(100050020010050201052);
    $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

  8. #8
    SitePoint Addict eanimator's Avatar
    Join Date
    Sep 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanksssssssss

  9. #9
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I got this...

    PHP Code:
    <?php
    $iTargetAmount 
    1250;
    while(
    $iCurrentChange != $iTargetAmount)
    {
        foreach(array(
    1000500200100502010521) 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.
    @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.

  10. #10
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by eanimator View Post
    Thanksssssssss
    At least make sure you understand it before turning it in as your solution

  11. #11
    SitePoint Addict eanimator's Avatar
    Join Date
    Sep 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    YeSS.. m doing it

  12. #12
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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

Posting Permissions

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