斐波那契数列记忆化递归

This commit is contained in:
2025-04-07 23:01:46 +08:00
parent 8b0ec58d27
commit d821e181e7
2 changed files with 43 additions and 0 deletions

21
others/test3.cpp Normal file
View File

@@ -0,0 +1,21 @@
// 斐波那契数列记忆化递归
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 3;
int dep[N];
int fib(int a){
if(a <= 2) return 1;
if(dep[a] != -1) return dep[a];
return dep[a] = fib(a-1) + fib(a-2);
}
signed main(){
fill(dep, dep + N, -1);
int n; cin >> n;
cout << fib(n) << endl;
return 0;
}
/*
1 1 2 3 5 8 13 21 34
*/