第一种(数组方式)
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long a[60], n;
cin >> n;
a[1] = 1;
a[2] = 1;
for (int i = 3; i <= n; i++)
{
a[i] = a[i - 1] + a[i - 2];
}
cout << a[n] << endl;
return 0;
}
第二种( 递归式求解)
#include <bits/stdc++.h>
using namespace std;
int main()
{
//A(n) = A(n - 1) + A(n - 2)
long long x, y, r, n;
cin >> n;
x = 1;
y = 1;
for (int i = 3; i <= n; i++)
{
r = x + y; //第x个
x = y;
y = r;
}
if (n == 1 || n == 2)
{
cout << 1 << endl;
}
else
{
cout << r << endl;
}
return 0;
}