提交时间:2024-10-24 10:32:40

运行 ID: 53576

#include<bits/stdc++.h> using namespace std; int a[21];//a[n]第n项fib数列的值 //记忆化递归 int f(int n) {//求第n项斐波那契数列的值 if(a[n] != 0) return a[n]; if(n == 1 || n == 2) return a[n] = 1; return a[n] = f(n - 1) + f(n - 2); } int main() { int n; cin >> n; cout <<f(n) << endl; return 0; }