时间限制: 1000 ms 空间限制: 262144 KB
题目描述
已知 f(x,n)=,输入x和n的值,计算f(x,n)的值。
输入
一行两个数x和n,其中x是实数,n是整数。1<=x,n<=20。
输出
输出f(x,n)的值,答案保留两位小数。样例输入
4.2 10
样例输出
3.68
数据范围限制
思路:
迭代
注意:虽然float可以过测试,但是为了保证精度还是建议使用double
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
double fn(int n,double x)
{
double t;
t=x+1;
for(int i=0;i<=n-1;i++)
{
t=sqrt(t);
if(i<=n-2)
t+=i+2;
}
return t;
}
int main()
{
int n;
double x;
cin>>x>>n;
cout.setf(ios::fixed);
cout << fixed<< setprecision(2) << fn(n,x) <<endl;
return 0;
}
参考代码:(C语言版)点击打开链接
明显简单一截而且迭代思路更加清晰,可见在下就是个菜鸟......
#include <stdio.h>
#include <math.h>
int main(void)
{
double f;
int n, i;
scanf("%lf%d", &f, &n);
for(i=1; i<=n; i++)
f = sqrt(i + f);
printf("%.2lf\n", f);
return 0;
}