Package ms100;/** * A step will always have an n level, assuming you can jump 1 levels at a time. can also jump 2 levels, to find the total number of total common jump method. And analyze the time complexity of the algorithm * Note: This problem is often seen in the near future. including MicroStrategy and other more attention to the algorithm of the company has selected a problem as a face test or pen test.First we consider the simplest case: if there are only 1 steps, there is obviously only one way to jump. Assuming there are 2 steps, there are two ways to jump: One is to jump two times. Jump 1 levels at a time, and the second one jumps 2 levels at a time. Now let's talk about the general situation: we think of the N-Step jumping method as a function of N. The memory is f (n).
When N>2. The first jump when there are two different choices: first, only jump 1, when the number of hops is equal to the rest of the n-1 steps of the number of jumps, that is f (n-1), the second option is the first jump 2, at this time the number of hops equal to the remaining n-2 steps of the number of hops, that is, F (n-2).
So the total number of different hops for n-Steps is f (n) = f (n-1) + f (n-2).
We summarize the above analysis with a formula such as the following: / 1 (n=1) f (n) = 2 (n=2) \ F (n-1) + (f-2) (n>2) */public Class Ms_27 {private int jumpstep (int n) {if (n<0) return 0;if (n==1| | n==2) return N;return jumpstep (n-1) + jumpstep (n-2);} public static void Main (string[] args) {ms_27 ms27 = new ms_27 (); System.out.println (Ms27.jumpstep (2));}}
"Microsoft 100" a step always jointly owned N-class, assuming that one can jump 1 levels, also can jump 2 levels, the total number of common total jump method, and analyze the time complexity of the algorithm