[Offoffer] Fibonacci Series

Source: Internet
Author: User

Question 1 Description:


Write a function and input n to obtain the nth entry of the Fibonacci series. The Fibonacci sequence is defined as follows:

F (n) = 0 (n = 0); F (n) = 1 (n = 1); F (n) = f (n-1) + f (n-2) (n> 1 );


Analysis description:


In most C language textbooks, the Fibonacci sequence is obtained recursively. The Code is as follows:

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

The advantage of recursive methods is that the Code is clear, but there is a more serious drawback, that is, repeated computing. On the P9 page of "data structure and algorithm analysis-C language description", four basic laws of recursion are introduced. Among them, 4th are "legal benefit rules". that is, when solving the same instance of a problem, do not perform repetitive work in different recursive calls. This rule is violated. Therefore, you must find another way to avoid repeated computing problems.

It can be calculated from the bottom up. First, F (2) is calculated based on F (0) and F (1), and then f (3) is calculated based on F (1) and F (2) ...... And so on, the nth item can be calculated.

long long Fibonacci(unsigned n){int result[2] = {0, 1};if(n < 2)return result[n];long long fibNMinusOne = 1;long long fibNMinusTwo = 0;long long fibN = 0;for(unsigned int i = 2; i <= n; ++i){fibN = fibNMinusOne = fibNMinusTwo;fibNMinusTwo = fibNMinusOne;fibNMinusOne = fibN;}return fibN;}

The time complexity of this method is O (n ).


Question 2 Description:


A frog can jump to level 1 or Level 2 at a time. Find the total number of hops that the frog jumps to an n-level step.


Analysis description:

Consider the jump Method of N-level steps as a function of N, and record it as F (n ). When N> 2, there are two different options for the first hop: one is the first hop with only one level. At this time, the number of hops equals to the number of hops for the next n-1 level step, that is F (n-1); another option is the first jump level 2, at this time the number of Jump method is equal to the number of the next step of the N-2 level, that is, F (n-2 ). Therefore, F (n) = f (n-1) + f (n-2) in the total number of different hops of N-level 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.