The offer-of the second chapter of the sword--the Fibonacci sequence (the frog jumps the steps)

Source: Internet
Author: User

Recursion and looping

Recursion: This function is called inside a function.

Essence: Divide a problem into two, or multiple small problems (overlapping parts of multiple small problems, there will be repeated computations)

Advantages: Simple, easy to implement.

Disadvantage: time and space consumption is serious, if the hierarchy of recursive calls too many, it will exceed the stack capacity.

Loop: Repeats an operation within a range by setting the initial value of the calculation and the termination condition.

Fibonacci sequence

Title One: Write a function, enter N, find the nth of the retracement (FIBONACCI) sequence, defined as follows:

The first solution: recursive algorithm:

Long long Fabonacci (unsigned int n) {if (n<=0) return 0;if (n==1) return 1;return Fabonacci (n-1) +fabonacci (n-2);}

When n=10 the call graph is as follows:

From the time we can see recursion, most of them have been repeatedly computed, which has a very negative effect on performance, and the time complexity of the algorithm is n exponent.

Second solution: Using loops (Time complexity is O (n))

#include <iostream>using namespacestd;Long LongFabonacci (unsignedintN) {    intarrary[2]={0,1}; Long Longfabn; if(n<2) Fabn=Arrary[n]; Long LongFabone=1; Long Longfabtwo=0;  for(unsignedintI=2; i<=n;++i) {fabn=fabone+Fabtwo; Fabtwo=Fabone; Fabone=fabn; }    returnfabn;}voidMain () {Long LongN=fabonacci ( the); cout<<n<<Endl;}


Title Two: A frog can jump up a step at a time, you can jump up to 2 steps, the frog jumped on the number of steps on the N-step method.

Idea: When there is only one step, the frog's jumping method is only one. When there are two steps, there are two kinds of frog jumping method (one is: Jump two steps, two: first-level jump). When there are n steps, the frog in the first take-off when jumping only a step, then there are still n-1 steps to jump, if the first jump on the two-step stage, then there is still the n-2 step of the jumping method. The whole question is exactly a Fibonacci sequence. The formula is as follows:

Topic Three: The cover of the Matrix, with eight 2*1 small matrix to cover a 2*8 large matrix. As shown in the following:

The first small matrix can have two coverage methods across, then at this time, it must be the second small matrix is also transverse, leaving the large matrix of 2*6, and then left the 2*7 of the large matrix needs to be covered. It is therefore possible to have: f (8) =f (6) +f (7).

The formula above the second question.

The offer-of the second chapter of the sword--the Fibonacci sequence (the frog jumps the 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.