B2147 求 f(x,n)
Content
求给定
Solution
乍一看这题目很烦人,其实,如果我们可以转换一下,这道题目就很简单。
我们不妨算下:
然后我们可以发现,
然而,如果
然后你就可以明白了,这不就可以用数组直接循环递推出来就可以了吗?你可能发现了第一维的
然后你就可以用递推通过本题了。
Code
1 递归
#include <cstdio>
#include <cmath>
using namespace std;
inline double f(double x, int n) {
if(n > 1) return sqrt(n + f(x, n - 1));
else return sqrt(1 + x);
}
int main() {
double x; scanf("%lf", &x);
int n; scanf("%d", &n);
return printf("%.2lf", f(x, n)), 0;
}
2 递推
#include <cstdio>
#include <cmath>
using namespace std;
int main() {
double x; scanf("%lf", &x);
int n; scanf("%d", &n);
double f[10007] = {0.0}; f[1] = sqrt(1 + x);
F(int, i, 2, n) f[i] = sqrt(i + f[i - 1]);
return printf("%.2lf", f[n]), 0;
}