Three methods for jumping step problem (abnormal jumping steps)

Source: Internet
Author: User

Topic 1: A step of a total of n-level, if you can jump 1 levels, you can jump 2 levels. How many total hops are in total, and the time complexity of the algorithm is analyzed.

We think of the N-Step jumping method as a function of N, which is recorded as f (n). When N>2, the first jump when there are two different choices: first, only jump 1, when the number of hops is equal to the rest of the n-1 steps of the number of jumps, that is f (n-1); Another option is the first jump 2, at this time the number of hops is equal to the rest of the n-2 step of the number of jumps, That is f (n-2). Thus the total number of different hops for N-Step steps f (n) =f (n-1) + (f-2).

We summarize the above analysis with a formula as follows:

/1 N=1
F (n) = 2 n=2
\ f (n-1) + (f-2) n>2

Analysis here, I believe a lot of people can see that this is our familiar Fibonacci sequence.

Here are my three ways to solve this:

#include <stdio.h>
#include <string.h>
Recursive solution
int solution1 (int n)
{
if (n = = 0 | | n = = 1) return 1;
else return Solution1 (n-1) + solution1 (n-2);
}
Non-recursive solution
int Solution2 (int n)
{
int f[100];
F[0] = 1;
F[1] = 1;
for (int i=2; i<=n; ++i)
F[i] = F[i-1] + f[i-2];
return f[n];
}
Method of scrolling Array
int Solution3 (int n)
{
int f[3]={0,1,2};
if (n<=2)
{
return f[n];
}
for (int i=3;i<=n;i++)
{
F[0]=F[1];
F[1]=F[2];
F[2]=F[0]+F[1];
}
return f[2];
}
int main ()
{
int n;
printf ("Please enter the number of steps:");
scanf ("%d", &n);
int sum1= solution1 (n);
int sum2= solution2 (n);
int sum3= solution3 (n);
printf ("sum1=%d,sum2=%d,sum3=%d\n", sum1,sum2,sum3);
return 0;
}

Topic 2: A step has a total of n, if you can jump 1 levels at a time, you can jump 2 levels ... It can also jump on n levels. At this point, the frog jumps to the level of the N-step of the total number of jumping method?
 Analysis: Using FIB (n) to indicate the frog jumps on the N-Step steps of the number of hops, the frog jumped on the N-Step step of the number of hops 1 (n-Step), set fib (0) = 1;
when n = 1 o'clock, there is only one method of jumping, that is, the 1-Step Jump: Fib (1) = 1;
when n = 2 o'clock, there are two ways of jumping, one-step and second-order jumps: fib (2) = FIB (1) + fib (0) = 2;
when n = 3 o'clock, there are three ways to jump, the first step out of one, followed by FIB (3-1) in the Jump method, the first second out of the second, there is a fib (3-2) in the Jump method, after the first jump out of third order, followed by FIB (3-3) in the Jump method
fib (3) = FIB (2) + fib (1) +fib (0) =4;
when n = N, there is a total of n jumps, the first step out of one order, followed by FIB (n-1) in the Jump method, after the first second out of the second, there is a fib (n-2) in the Jump method ..... ..... After the first step out of the N-order, there is a Fib (n-n) jump in the back.
fib (n) = fib (n-1) +fib (n-2) +fib (n-3) +..........+fib (n-n) =fib (0) +fib (1) +fib (2) +.......+fib (n-1)
and because Fib (n-1) =fib (0) +fib (1) +fib (2) +.......+fib (n-2)
two-type subtraction: Fib (n)-fib (n-1) =fib (n-1) ===== "Fib (n) = 2*fib (n-1)N >= 2
The recursive equation is as follows:

Freak Jump
int solution4 (int n)
{
if (n = = 0 | | n = = 1)
return 1;
Else
Return 2*solution4 (n-1);
}

Perverted jump Scrolling Array
int solution5 (int n)
{
int f[2]={0,1};
if (n < 2)
return f[n];

for (int i=2;i<=n;i++)
{
F[0]=F[1];
F[1]=2*F[0];
}
return f[1];
}

Three methods for jumping step problem (abnormal jumping steps)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.