C ++ implementation Fibonacci number recursively

Shortly just learning from a small konjac,

The first grant blog;

Remember zhx said:

"To understand recursion, you must first understand recursion."

emmm really not one thing wrong with ah! ! !

It is a recursive function,

Stop calling themselves,

In order to prevent an infinite loop,

Stuttgart needed sentenced;

Recursion is mainly to find a formula that is similar to something ( I do not know Jiaosha );

Then write a function that calls itself;

 1 #include<iostream>
 2 using namespace std;
 3 int fib(int);
 4 int main()
 5 {
 6     int n,a[200];
 7     cin>>n;
 8     for(int i=1;i<=n;i++)
 9         cin>>a[i];
10     for(int i=1;i<=n;i++)
11     {
12         cout<<fib(a[i])<<endl;
13     }
14     return 0;
15 }
16 int fib(int x)
17 {
18     if(x<=0) return 0;
19     if(x<=2) return 1;
20     return fib(x-1)+fib(x-2);
21 }

Daily punch O (∩_∩) O

Guess you like

Origin www.cnblogs.com/sxy2004/p/11450492.html