[Basis of algorithm design and analysis] 3. Fibonacci series, algorithm design and analysis
Package cn. xf. algorithm. ch02; import java. util. vector;/*** Fibonacci series * @ author xiaof **/public class Fib {/*** method 1, recursive Computing is very inefficient at double exponential level (don't ask me how to calculate it, I won't) * calculation method: baidu = "@ * @ param n * @ return */public static Long fiber 1 (int n) {if (n <= 1) return (long) n; elsereturn fiber 1 (n-1) + fiber 1 (n-2);} public static Long fiber 2 (int n) {Long f0 = 0l; long f1 = 1l; // Long fiber n = f1 + f2; Long fiber n = 0l; for (int I = 2; I <= n; ++ I) {fiber n = f0 + f1; f0 = f1; f1 = fiber n;} return fiber n;} public static Vector <Long> fiber 3 (int n) {Vector <Long> fib = new Vector <Long> (); fib. setSize (n + 1); fib. set (0, 0l); fib. set (1, 1l); for (int I = 2; I <= n; ++ I) {fib. set (I, fib. get (I-1) + fib. get (I-2);} return fib;} public static void main (String [] args) {System. out. println (Fib. fiber 1 (10); System. out. println ("******************************"); System. out. println (Fib. fiber 2 (10); System. out. println ("******************************"); vector <Long> result = Fib. fiber 3 (10); for (Long l: result) {System. out. print (l + "\ t ");}}}