[Basis of algorithm design and analysis] 3. Fibonacci series, algorithm design and analysis

Source: Internet
Author: User

[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 ");}}}

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.