Fibonacci sequence is a very important concept , which is given in Baidu Encyclopedia " Fibonacci sequence (Fibonacci sequence), also known as the golden section sequence, due to mathematician Leonardo Fibonacci Leonardoda Fibonacci was introduced with the example of rabbit breeding, so it is also called " rabbit sequence ", which refers to such a sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, ... Mathematically, the Fibonacci sequence is defined recursively as follows : F(1)=1, F(2)=1, F(n)=F(n-1)+F(n-2) (n>2, n∈N*) In the fields of modern physics, quasi-crystal structure, and chemistry, the Fibonacci sequence has direct applications. A mathematical journal under the name of Number Sequence Quarterly , dedicated to publishing research results in this area. ”
【General term formula calculation】
#includeusing namespace std; double Fibon (int n) { if (n < 1) { cout<<"n is error!"< >n; cost<
【Calculation by cycle method】
#includeusing namespace std; int Fibon (int n) { if (n < 0) { printf("n is error!\n"); return -1; } int n1 = 1; int n2 = 2; int n3 = 3; for (int i = 3;i <= n;++i) { n3 = n1+n2; n1 = n2; n2 = n3; } return n3; } intmain() { int n; cout<<"Please input Fibon index:"< >n; cout<<"Fibonacci Number 1"<
【Calculate recursively】
#includeusing namespace std; int Fibon (int n) { if (n < 0) { printf("n is error!\n"); return -1; } int n1 = 1; int n2 = 2; int n3 = 3; for (int i = 3;i <= n;++i) { n3 = n1+n2; n1 = n2; n2 = n3; } return n3; } intmain() { int n; cout<<"Please input Fibon index:"< >n; cout<<"Fibonacci Number 1"<