题解 | #求解立方根#
求解立方根
http://www.nowcoder.com/practice/caf35ae421194a1090c22fe223357dca
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double v = scanner.nextDouble();
double re = 0;
if(v==0)
re = 0;
double x0,x1;
x0=v;
x1=(2*x0/3)+(v/(x0*x0*3));
while(Math.abs(x1-x0)>0.000001){
x0=x1;
x1=(2*x0/3)+(v/(x0*x0*3));
}
re = x1;
double v1 = Math.round(re * 10) / 10d;
System.out.println(v1);
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double v = scanner.nextDouble();
double re = 0;
if(v==0)
re = 0;
double x0,x1;
x0=v;
x1=(2*x0/3)+(v/(x0*x0*3));
while(Math.abs(x1-x0)>0.000001){
x0=x1;
x1=(2*x0/3)+(v/(x0*x0*3));
}
re = x1;
double v1 = Math.round(re * 10) / 10d;
System.out.println(v1);
}
}