如下:
#include <iostream>
#include <cstdio>
using namespace std;
int f1(int x) { //普通递归
if (x <= 1)
return 1;
else
{
return f1(x - 1) + f1(x - 2);
}
}
int f2(int x) { //递归改进
int v[10];
v[0] = v[1] = 1;
for (int i = 2; i < x; i++)
v[i] = -1;
if (v[x] < 0)
v[x] = f2(x - 1) + f2(x - 2);
return v[x];
}
int f3(int x) { //动态规划
int a[10];
int i;
a[0] = a[1] = 1;
for (i = 2; i <= x; i++) {
a[i] = a[i - 1] + a[i - 2];
}
return a[x];
}
int main() {
int x;
cin >> x;
int num = f3(x); //f1()、f2()
cout << num << endl;
return 0;
}