题解 | #求解立方根#
求解立方根
https://www.nowcoder.com/practice/caf35ae421194a1090c22fe223357dca
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); double v = in.nextDouble(); double result = newton(v, 1, 0, 10); result = Math.round(result * 10) / 10.0; System.out.println(result); } private static double newton(double v, double initDot, int nowCnt, int totalCnt) { if (nowCnt == totalCnt) { return initDot; } double v1 = initDot * initDot * initDot - v; double v2 = 3 * initDot * initDot; // x1 = x0-f(x)/f'(x) double newDot = initDot - v1 / v2; return newton(v, newDot, nowCnt + 1, totalCnt); } }
牛顿迭代法:
公式:x1=x0-f(x)/f'(x);
具体参考这篇博客:https://blog.csdn.net/weixin_41346635/article/details/115404645