1 /// <summary> 2 /// 上楼梯算法(迭代) 3 /// </summary> 4 /// <param name="n"></param> 5 /// <returns></returns> 6 private static long fib3(int n) 7 { 8 long x = 0, y = 0, z = 1; 9 long w, k; 10 11 for (int j = 0; j < n; j++) 12 { 13 w = z; 14 k = y; 15 z = x + y + z; 16 y = w; 17 x = k; 18 } 19 return z; 20 } 21 22 /// <summary> 23 /// 上楼梯算法(递归) 24 /// </summary> 25 /// <param name="n"></param> 26 /// <returns></returns> 27 private static long dfib1(int n) 28 { 29 if (n < 1) 30 return 0; 31 if (n == 1) 32 return 1; 33 if (n == 2) 34 return 2; 35 if (n == 3) 36 return 4; 37 38 return dfib1(n - 1) + dfib1(n - 2) + dfib1(n - 3); 39 }