输入x,n,其中x是浮点数,n为正整数,不超过100
输出函数值f(x,n),保留两位小数
解题思路:
可以根据图片观察出一直加到n=1,而且一直嵌套,那么我们可以从右边往左边开始算,
先求出sqrt(1+x),把这个所求的值作为x继续套下去,直到从1加到n,代码如下
#include <iostream>
#include <iomanip> //保留两位小数调用的头文件
using namespace std;
int main()
{
int n;
float x;
while (cin >> x >> n)
{
cout << fixed << setprecision(2); // 后续输出都保留两位小数
double a;
for (int i = 1; i <= n; i++) //从右边往左边算
{
a = sqrt(i + x); //从i=1算到i=n
x = a; // 所求的a作为x继续循环
}
cout << a << endl;
}
}