Code VS1842 递归第一次(silver)

版权声明:版权归Ordinarv所有 https://blog.csdn.net/ordinarv/article/details/82015247

题意

递归求解下面函数,n>=-30;

f(x)=5 (x>=0)

f(x)=f(x+1)+f(x+2)+1 (x<0)

直接递归求解即可。

但是这种负数如何记忆化搜索呢,将负数映射到正数?


AC Code

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=30;
const int INF=0x3f3f3f3f;
ll f[maxn+10];
ll solve(int x){
	if(x>=0) return 5;
	//if(f[x]) return f[x];
	return solve(x+1)+solve(x+2)+1;	
}
int main() {
	int n;
	cout<<solve(n)<<endl;
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/ordinarv/article/details/82015247