Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Oct 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question A very simple question

    Hey,
    I have a very simple question abt the Rabbit Jumping problem.

    If a rabbit need to across the river, there are number of stones positioned at different distance between the two opposite side of a river, the rabbit can only hop at most 50 centimetres far. I suppose to write out a program to calculate the minimum number of jumps it needs to take to reach the other side of the river.

    But I stuck at the algorithm of calculating the min jumps.... Can anyone give me some ideas. Thanks in advance.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You may use the modulo operator (I guess in fact that was the intention of you teacher):

    <script type="text/javascript">
    var riverWidth=13.4;
    var maxJump=0.5;
    var d=parseInt(riverWidth/maxJump);
    var minJumps=riverWidth%maxJump==0?d:d+1;
    alert(minJumps);
    </script>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New Coder
    Join Date
    Oct 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Em... but how to use array to store the number of stones between the river?

    AND what does this " 0?d:d+1 " mean?

    Thanks

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    The stones number is, obviousely, the min jumps-1.

    condition?statement1:statement2
    is a ternary operator. It is the shorthand notation for

    if(condition){
    statement1
    }
    else{
    statement2
    }
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    New Coder
    Join Date
    Oct 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey, thanks
    I almost done... but I have a problem...
    For example: if the user enter the distance measured from one side of the river like this : 5 10 15 20 25 100 , it is impossible for the rabbit to jump from the 2nd last stone(which is positioned at 25cm measured from the starting side of the river) to 100, right?
    OR if the user entered 40 70 150 160 180, it is also impossible for the rabbit to jump from 70 to 150, thus the rabbit cannot jump across the river...

    How do we check huh?
    Do we have to write a loop to check the difference between every stones?

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    I don't understand your problem.

    For me, the logical analysis is like that:

    - I have the width of the river and the max witdh of rabbits jump
    - I need to find the number of the stones.

    To find that, I find as a first step the number of minimum jumps the rabbit must do (I have shown you the code). Now I substract 1 from that number (but only if modulo between Riverwidth/maxWidhOfAJump is different from 0) and here's the result, the number of the stones.
    Last edited by Kor; 10-20-2006 at 02:03 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    New Coder
    Join Date
    Oct 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry 4 not explaining clearly...
    but thanks, I finally get it.

  • #8
    New Coder
    Join Date
    Oct 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hey,
    I've nominated u 4 ur help,
    Thanx~~


  •  

    Posting Permissions

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