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?
- /*----------------------------
- 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 <", there are a total of" <JumpStep (nStep) <"Skip method." <endl;
- Return 0;
- }
/* -------------------------- 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 ;}