1 package cn.test;
2
3 public class Test05 {
4 public static void main(String[] args) {
5 Test05 test05 = new Test05();
6
7 System.out.println(test05.JumpFloor(10));
8 System.out.println(test05.test(10));
9 System.out.println(test05.testJumpFloor(10));
10
11 }
12 //一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)
13 public int JumpFloor(int target) {
14 int result = 0;
15 int num1 = 1;
16 int num2 = 2;
17
18 if(target == 0) return 0;
19 if(target == 1 || target == 2) return target;
20
21
22 return JumpFloor(target-1) + JumpFloor(target-2);
23 }
24 //下面市县结果和上面是一样的
25 public int test(int n) {
26 int result = 0;
27
28 int left = 0;
29 int right = 1;
30
31 if(n == 0) return 0;
32 if(n == 1 || n == 2) return n;
33
34 for (int i = 0; i < n; i++) {
35 result = left + right;
36 left = right;
37 right = result;
38 }
39 return result;
40 }
41
42 //一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
43 public int testJumpFloor(int target) {
44 //n级台阶:2的n-1次方
45 /*
46 f(n) = f(n-1)+f(n-2)+...+f(1) + f(0)
47 就会是下面的结果
48 f(0) + f(1) + f(2) + f(3) + ... + f(n-1) == f(n) = 2*f(n-1)
49 */
50 return (int) Math.pow(2, target - 1);
51 }
52 }