斐波那契数列(递归与非递归)

版权声明:Andy https://blog.csdn.net/Alibaba_lhl/article/details/83036462
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e2+10;
ll a[MAXN];
ll F[MAXN];
ll f(ll n) ///递归
{
    if(F[n]>0) return F[n];
    if(n==1 || n==2) return 1;
    else return F[n] = f(n-1) + f(n-2);
}
int main()
{
    ll n;
    cin>> n;
    cout<< f(n) << endl;
    a[1] = a[2] = 1; ///非递归
    for(int i=3; i<=100; i++)
    {
        a[i] = a[i-1] + a[i-2];
    }
    cout<< a[n] << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Alibaba_lhl/article/details/83036462