题解 | #求解立方根#
求解立方根
http://www.nowcoder.com/practice/caf35ae421194a1090c22fe223357dca
#include <stdio.h>
#include <string.h>
#include <math.h>
// 或者牛顿法迭代 键入值为y
// 令 fx = x^3-y
// xk+1= xk - f(xk)/f'(xk)=xk - (xk^3-y)/3xk^2
// 当fx = x^3-y<1*10^-5停止迭代
double espi = 0.000001;
int main()
{
double y;
scanf("%lf", &y);
double x=1.0;
double fx = x*x*x-y;
while(fabs(fx)>0.0001){
x = x- fx/(3*x*x);
fx = x*x*x-y;
}
printf("%.1lf",x);
return 0;
}