Interview Question 9: Fibonacci Series

Source: Internet
Author: User


 

Method 1: It is easy to think of using recursion directly.

C ++ code:

 

#include "stdafx.h"   #include <iostream>   using namespace std;    long long Fibonacci(unsigned int n)  {      if (n == 0)      {          return 0;      }        if (n == 1)      {          return 1;      }        return Fibonacci(n-1) + Fibonacci(n-2);  }    int _tmain(int argc, _TCHAR* argv[])  {      unsigned int n = 10;      cout << Fibonacci(n) << endl;      system("pause");      return 0;  }  #include "stdafx.h"#include <iostream>using namespace std;long long Fibonacci(unsigned int n){    if (n == 0)    {return 0;    }if (n == 1){return 1;}return Fibonacci(n-1) + Fibonacci(n-2);}int _tmain(int argc, _TCHAR* argv[]){unsigned int n = 10;cout << Fibonacci(n) << endl;system("pause");return 0;}

Disadvantage: it is obvious that the efficiency is very low because of repeated computing problems.

 


Method 2: the improved method is to save the intermediate items of the obtained series and directly search for them next time to avoid repeated computation.

C ++ code:

 

#include "stdafx.h"   #include <iostream>   using namespace std;    long long Fibonacci(unsigned int n)  {      if (n == 0)      {          return 0;      }        if (n == 1)      {          return 1;      }        long long one = 0;      long long two = 1;      long long result = 0;        for (unsigned int i=2; i<=n; i++)      {          result = one + two;          one = two;          two = result;      }        return result;  }    int _tmain(int argc, _TCHAR* argv[])  {      unsigned int n = 100;      cout << Fibonacci(n) << endl;      system("pause");      return 0;  }  #include "stdafx.h"#include <iostream>using namespace std;long long Fibonacci(unsigned int n){    if (n == 0)    {return 0;    }if (n == 1){return 1;}long long one = 0;long long two = 1;long long result = 0;for (unsigned int i=2; i<=n; i++){result = one + two;        one = two;two = result;}return result;}int _tmain(int argc, _TCHAR* argv[]){unsigned int n = 100;cout << Fibonacci(n) << endl;system("pause");return 0;}

 

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.