题目描述:
立方根的逼近迭代方程是 y(n+1) = y(n)2/3 + x/(3y(n)*y(n)),其中y0=x.求给定的x经过n次迭代后立方根的值。
输入:
输入有多组数据。
每组一行,输入x n。
输出:
迭代n次后的立方根,double精度,保留小数点后面六位。
样例输入:
4654684 1
65461 23
样例输出:
3103122.666667
40.302088
实现代码:
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <algorithm>
#include <math.h>
using namespace std;
double curb(double x,int n){
if(n==0){
return x;
}else{
double k=curb(x,n-1);
return k/3.0*2.0+x/3.0/k/k;
}
}
int main()
{
double x;
int n;
while(scanf("%lf%d",&x,&n)!=EOF){
printf("%.6f\n",curb(x,n));
}
return 0;
}