Rabbit problem (Rabbit problem)

Description
There is a rabbit, a month after birth can grow up, grow up and then another month one pair of rabbits can be the birth of a small rabbit and every month after the birth of a pair. Now, we have this one pair of newborn rabbits, then, after n months, how many rabbits we have it? Assuming that all of the rabbits will not die.

Input

Input file only one row, comprising a natural number n.

Output

Output file only one line, contains a natural number, namely the number of rabbits n months.

Sample Input 1

1
Sample Output 1

1
Sample Input 2

3
Sample Output 2

2
- From YCOJ
simple recursive.

Here Insert Picture Description
Then we can the recurrence

#include<bits/stdc++.h>
using namespace std;
int a[100];

int fib(long long n){
if(a[n]==0){
    a[n]=fib(n-1)+fib(n-2);
  }
  return a[n];
 }
 int main(){
    a[1]=a[2]=1;
    long long n;
    cin >> n;
    cout << fib(n);
    return 0;
}

Guess you like

Origin www.cnblogs.com/A-Konnyaku/p/10993810.html