[Sword refers to Offer] 2. abnormal jump steps, sword refers to offer

Source: Internet
Author: User

[Sword refers to Offer] 2. abnormal jump steps, sword refers to offer

Question

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

Ideas

Use Fib (n) to represent the number of hops on the n-step. Set Fib (0) to 1;

When n = 1, there is only one hop method, that is, level 1 hop, that is, Fib (1) = 1;

When n = 2, there are two hop modes: first hop and second hop, that is, Fib (2) = Fib (1) + Fib (0) = 2;

When n = 3, there are three ways to skip. After jumping out of the first step for the first time, there is also the Fib (3-1) Middle hop method. After jumping out of the second step for the first time, there is also the Fib (3-2) Middle hop method in the back. After the first jump out of the third step, there is also the Fib (3-3) Middle hop method in the back, that is, Fib (3) = Fib (2) + Fib (1) + Fib (0) = 4;

When n = n, there are n skip methods. After jumping out of the first step for the first time, there is also the Fib (n-1) Middle hop method. After jumping out of the second step for the first time, there is a Fib (n-2) in the back of the jump .......................... After the first jump out of the n step, there is a Fib (n-n) in the jump method, that is, 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) So Fib (n) = 2 * Fib (n-1) n> = 2

To sum up:

Code

/* ------------------------------------- * Date: 2015-07-19 * Author: SJF0115 * Title: 2. abnormal jump steps * Web site: http://www.nowcoder.com/books/coding-interviews/22243d016f6b47f2a6928b4313c85387? Rp = 1 * result: AC * Source: offoff * blog: ----------------------------------------- */# include <iostream> using namespace std; class Solution {public: int jumpFloorII (int number) {if (number <= 0) {return 0;} // if else if (number = 1) {return 1 ;} // else return 2 * jumpFloorII (number-1) ;}}; int main () {Solution s; int number = 5; cout <s. jumpFloorII (number) <endl; return 0 ;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.