动态规划:
递归:
cpp
#include <iostream>
using namespace std;
int ans;
int n;
void f(int k)
{
if(k==n)ans++;
else if(k<n)
{
f(k+1);
f(k+2);
}
}
int main()
{
cin >> n;
f(0);
cout << ans;
return 0;
}
递推:
cpp
#include <iostream>
using namespace std;
const int N = 25;
int n,f[N];
int main()
{
cin >> n;
f[0]=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=3;j++)
{
if(i-j>=0)
f[i]+=f[i-j];
}
}
cout << f[n];
return 0;
}