题解 | 牛顿迭代法 #求解立方根#
求解立方根
https://www.nowcoder.com/practice/caf35ae421194a1090c22fe223357dca
import java.io.*; import java.util.*; /** * @author hope * @date 2023/8/28 - 19:12 */ public class Main { // 牛顿法求方程 x^3 - v = 0 的零点即可 // x 的迭代: // x = x - (x^3 - v) / (3x^2) public static void main(String[] args) { Scanner in = new Scanner(System.in); double v = in.nextDouble(); double x = 1.0; while (Math.abs(x * x * x - v) > 1e-6) { x = x - (x * x * x - v) / (3 * x * x); } System.out.printf("%.1f\n", x); } }