注意
自行复制链接去进行刷题,因为平台的跳转功能太差。
https://www.acwing.com/problem/content/823/
https://www.acwing.com/solution/content/135651/
f1 普通的递归
#include <bits/stdc++.h>
// 2024-03-04 Come on !
using namespace std;
#define ll long long
int solve(int n){//cout << "Hello world !" << '\n';if(n==1||n==0){return 1;}else return solve(n-1)+solve(n-2);
}
int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); // no endll; int t=1;while(t--){int n;cin>>n;cout<<solve(n);}return 0;
}
f1_pro 记忆
#include <bits/stdc++.h>
// 2024-03-04 Come on !
using namespace std;
#define ll long long
int mem[20],sum;
int solve(int n){//cout << "Hello world !" << '\n';if(mem[n]) return mem[n];if(n==1||n==0){return 1;}else sum=solve(n-1)+solve(n-2);mem[n]=sum;return sum;
}
int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); // no endll; int t=1;while(t--){cout<<solve(5);}return 0;
}
f1_pro_plus 递推
#include <bits/stdc++.h>
// 2024-03-04 Come on !
using namespace std;
#define ll long long
int f[20],sum,n;int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); // no endll; cin>>n;f[1]=1;f[2]=2;if(n==1||n==2)cout<<f[n];for(int i=3;i<=n;i++){f[i]=f[i-2]+f[i-1];//TODO}cout<<f[n];return 0;
}
f1_pro_plus_pro 空间优化
#include <bits/stdc++.h>
// 2024-03-04 Come on !
using namespace std;
#define ll long long
int f[20],sum,n;int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); // no endll; cin>>n;f[1]=1;f[2]=2;if(n==1||n==2)cout<<f[n];int nb = 0 ,tmp1=1, tmp2= 2;for(int i=3;i<=n;i++){nb= tmp1+ tmp2;tmp1=tmp2;tmp2=nb;}cout<<nb;return 0;
}