首先,大家都知道递归
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
ll n;
ll f(ll a,ll b){
if(a >= b) return 1;
else return f(a + 1,b) + f(a + 2,b);
}
int main(){
cin >> n;
cout << f(1,n);
return 0;
}
上面就是一个典型的例子
题目在这↓
https://oj.aicoders.cn/problem/3101
但是数大的时候,他就会递归不过来
所以我们今天来讲一种递归:记忆化递归
它正好能弥补普通递归的弊端
代码大约如下↓
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
ll a,x[1000];
void f(ll b,ll c){
if(b == c) x[b] = b;return;
x[b] = b;
f(b + 1,c);
}
int main(){
cin >> a;
f(0,a);
for(ll i = 0;i < a;i += 1){
cout << x[i];
}
return 0;
}
原理就是将遍历过的数存到数组里再输出
353

被折叠的 条评论
为什么被折叠?



