首页 > 其他 > 详细

求解立方根

时间:2020-07-03 13:29:13      阅读:64      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

 

 

System.out.println(Math.pow(input, 1.0/3));

牛顿迭代法

技术分享图片

 

import java.util.*;

public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        double in = sc.nextDouble();
        double res = getCubeRoot(in);
        //保留一位小数
        System.out.println(String.format("%.1f",res));
    }
    public static double getCubeRoot(double input){
        if(input == 0){
            return 0;
        }else{
            double x0,x1;
            x0 = input;
            x1 = (2*x0 + input/x0/x0)/3;
            while(Math.abs(x1 - x0) > 0.000001){
                x0 = x1;
                x1 = (2*x0 + input/x0/x0)/3;
            }
            return x1;
        }

    }
}

 

求解立方根

原文:https://www.cnblogs.com/gy7777777/p/13229544.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!