A step has a total of n levels. If you can skip 1 level at a time, you can also skip 2 levels.

Source: Internet
Author: User
Question: a step has a total of n levels. If you can skip 1 level at a time, you can also skip 2 levels to find out the total number of hops in total and analyze the time complexity of the algorithm. Note: This question has frequently appeared recently. Companies that focus on algorithms, such as MicroStrategy, have chosen this question as an interview question or a pen question. Thought 1: first, we should consider the simplest

Question: a step has a total of n levels. If you can skip 1 level at a time, you can also skip 2 levels to find out the total number of hops in total and analyze the time complexity of the algorithm. Note: This question has frequently appeared recently. Companies that focus on algorithms, such as MicroStrategy, have chosen this question as an interview question or a pen question. Thought 1: first, we should consider the simplest

Question:

A step has a total of n levels. If you can skip 1 or 2 at a time, you can find the total number of hops in total and analyze the time complexity of the algorithm.

Note:
This question has frequently appeared recently, and companies that focus on algorithms, such as MicroStrategy, have chosen this question as an interview question or a pen question.

Thought 1:

First, we should consider the simplest situation: if there is only one level, there is obviously only one method of jumping. If there are two levels, there are two methods of jumping: one is two hops, one for each hop and two for each hop.
Now let's discuss the general situation: Let's take 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 for n-level steps ).
We will summarize the above analysis using a formula as follows:
/1 (n = 1)
F (n) = 2 (n = 2)
\ F (n-1) + (F-2) (n> 2)
From this analysis, I believe many people can see that this is the familiar sequence of fiber ACCI. (O (n ))

The Code is as follows:

[Cpp]View plaincopyprint?

  1. /*----------------------------
  2. Copyright by yuucyf. 2011.08.16
  3. -----------------------------*/
  4. # Include "stdafx. h"
  5. # Include
  6. Using namespace std;
  7. Int JumpStep (int n)
  8. {
  9. If (n <= 0) return 0;
  10. If (n = 1 | n = 2) return n;
  11. Return (JumpStep (n-1) + JumpStep (n-2 ));
  12. }
  13. Int _ tmain (int argc, _ TCHAR * argv [])
  14. {
  15. Int nStep = 0;
  16. Cout <"Enter the number of steps :";
  17. Cin> nStep;
  18. Cout <"number of steps" <nStep <", there are a total of" <JumpStep (nStep) <"Skip method." <endl;
  19. Return 0;
  20. }

/* -------------------------- Copyright by yuucyf. 2011.08.16 ------------------------------- */# include "stdafx. h" # include
 
  
Using namespace std; int JumpStep (int n) {if (n <= 0) return 0; if (n = 1 | n = 2) return n; return (JumpStep (n-1) + JumpStep (n-2);} int _ tmain (int argc, _ TCHAR * argv []) {int nStep = 0; cout <"Enter the number of steps:"; cin> nStep; cout <"number of steps" <nStep <", then there is a total of" <JumpStep (nStep) <"Skip method. "<endl; return 0 ;}
 

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.