题解 | #求解立方根#
求解立方根
http://www.nowcoder.com/practice/caf35ae421194a1090c22fe223357dca
//牛顿迭代公式:
//xn+1=xn-f(xn)/f'(xn)
//即
//x=x-(x^3-y)/(3*x^2)
// =x-x^3/(3*x^2)+y/(3*x^2)
// =2*x/3+y/x/x/3
// =(2*x+y/x/x)/3;
double cubeRoot(double n);
double cubeRoot(double n) {
double x;
for(x = 1.0; fabs(x*x*x-n) > 1e-7; x = (2*x+n/x/x)/3)
;
return x;
}
int main(void) {
double n;
while(~scanf("%lf", &n)) {
printf("%.1lf\n", cubeRoot(n));
}
}