A frog can jump up to 1 steps at a time , or jump up to level 2 . How many hops does the frog have to jump on an n-level stair altogether?
Solution: PutNthe jumping method of the step isf (N), whenn>2, there are two different choices when jumping for the first time: first, jumping only1the number of hops equals the rest of the back.n-1the number of jumps of a step, that isf (n-1);the other is the first jump .2the number of hops equals the rest of the back.n-2the number of jumps of a step, that isf (n-2);soNthe jumping method of the step isf (N) =f (n-1) +f (n-2). It is not hard to see that this is actually a variant application of the Fibonacci sequence, which moves each of the Fibonacci sequence forward.1bit.
Program:
#include <stdio.h>
int Fibonacci (int n)
{
int NUM1 = 1, num2 = 1, num3 = 0, i = 0;
if (n <= 1)
{
return NUM1;
}
for (i = 1; i < n; i++)
{
num3 = Num1 + num2;
NUM1 = num2;
num2 = num3;
}
return num3;
}
int main ()
{
int num = 0, ret = 0;
printf ("Please enter the number of steps:");
scanf ("%d", &num);
ret = Fibonacci (num);
printf ("A total of%d hops!") \ n ", ret);
return 0;
}
Results:
Please enter the number of steps : 3
There are 3 Different kinds of jumping methods!
Please press any key to continue ...
expansion: Span style= "FONT-FAMILY:CALIBRI;" >1 steps can also jump on 2 level, n class. Ask the frog to jump to the previous n level steps how many kinds of jumping?
Solution: We can obtain a total of f (n) =2^ (n-1) by mathematical induction method
Program:
#include <stdio.h>
#include <math.h>
int Fibonacci (int n)
{
Return pow (2,n-1);
}
int main ()
{
int num = 0, ret = 0;
printf ("Please enter the number of steps:");
scanf ("%d", &num);
ret = Fibonacci (num);
printf ("A total of%d hops!") \ n ", ret);
return 0;
}
Results:
Please enter the number of steps : 3
There are 4 Different kinds of jumping methods!
Please press any key to continue ...
This article is from the "Rock Owl" blog, please be sure to keep this source http://yaoyaolx.blog.51cto.com/10732111/1742286
C: A frog can jump up to 1 steps at a time, or jump up to level 2. How many hops does the frog have to jump on an n-level stair altogether?