题解 | #求解立方根#
求解立方根
http://www.nowcoder.com/practice/caf35ae421194a1090c22fe223357dca
数学迭代问题
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); while (sc.hasNext()){ double input = sc.nextDouble(); double n = getCubeRoot(input); System.out.printf("%.1f",n); } } public static double getCubeRoot(double input) { if ( input == 0) { return 0; } double x0 = input; double x1 = (2*x0 + input/x0/x0)/3; while (Math.abs(x1 - x0) > 0.001) { x0 = x1; x1 = (2*x0 + input/x0/x0)/3; } return x1; } }