Prove safety offer7: Fibonacci column n items (from 0, the first 0 is 0)

1. Title Description

  We all know that Fibonacci number, and now asked to enter an integer n, you output the n-th Fibonacci number Fibonacci sequence of item (from 0, the first 0 is 0). n <= 39

2. The ideas and methods

  Fibonacci number (Fibonacci sequence), also known as golden columns, because mathematician Leonardo Fibonacci (Leonardoda Fibonacci) as an example to the rabbit breeding and the introduction, it is also known as "Rabbit series," referring to such a series is: 1,1,2,3,5,8,13,21,34, ...... mathematically as follows fibonacci recursion method is to define: F (1) = 1, F (2) = 1, F (n) = F (n-1) + F (n-2) (n> = 3, n∈N *).

  In this problem, there is a condition that " starting from 0 of 0 0 ", starting from 0, and therefore, F (0) = 0, F (1) = 1, F (n) = F (n-1 ) + F (n-2) (n> = 2, n∈N *).

3.C ++ Code

 1 class Solution {
 2 public:
 3     int Fibonacci(int n) {
 4         if (n <= 1)
 5              return n;
 6          int res = 0;
 7          int n1 = 0;
 8          int n2 = 1;
 9          for (int i=2; i<=n; i++){
10              res = (n1 + n2);
11              n1 = n2;
12              n2 = res;           
13          }
14         return res;
15     }
16 };
View Code

Reference material

https://blog.csdn.net/sun_fengjiao/article/details/88094461

Guess you like

Origin www.cnblogs.com/wxwhnu/p/11407123.html