Solution of Frog Jumping Steps in php
- 2021-11-10 08:59:43
- OfStack
A frog can jump up one step or two steps at a time. Find out how many jumping methods the frog has when jumping on a step of n level (different results are calculated according to different sequences).
Thoughts:
Find the law f (1) = 1 f (2) = 2 f (3) = 3 f (4) = 5 f (n) = f (n-1) + f (n-2) This is a Fibonacci sequence
2. Because when you adjust to the n step, the penultimate step can jump in one step, and the penultimate step can also jump in one step
Non-recursive version:
JumpFloor(target)
if target==1 || target==2 return target
jumpSum=0
jump1=1
jump2=2
for i=3;i<target;i++
jumpSum=jump1+jump2
jump1=jump2
jump2=jumpSum
return jumpSum
function jumpFloor($number)
{
if($number==1 || $number==2){
return $number;
}
$jumpSum=0;
$jump1=1;
$jump2=2;
for($i=3;$i<=$number;$i++){
$jumpSum=$jump1+$jump2;
$jump1=$jump2;
$jump2=$jumpSum;
}
return $jumpSum;
}
$res=jumpFloor(10);
var_dump($res);
The above code examples can be tested locally. Thank you for your support of this site.