Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 8 of 8
Thread: A very simple question

10202006, 10:57 AM #1
 Join Date
 Oct 2006
 Posts
 12
 Thanks
 0
 Thanked 0 Times in 0 Posts
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.
10202006, 11:08 AM
#2
 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>
10202006, 11:35 AM
#3
 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
10202006, 11:59 AM
#4
 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 jumps1.
condition?statement1:statement2
is a ternary operator. It is the shorthand notation for
if(condition){
statement1
}
else{
statement2
}
10202006, 12:54 PM
#5
 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?
10202006, 12:59 PM
#6
 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; 10202006 at 01:03 PM.
10202006, 01:45 PM
#7
 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.
10202006, 01:49 PM
#8
 Join Date
 Oct 2006
 Posts
 12
 Thanks
 0
 Thanked 0 Times in 0 Posts
hey,
I've nominated u 4 ur help,
Thanx~~