HJ107 求解立方根

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

 

public class Main{  

    public static void main(String[]args) throws IOException {

        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        double input=Double.parseDouble(br.readLine());

        boolean flag=false;

        if(input<0) {

            flag=true;

            input=-input;

        }

        double distance=input;//误差或者增加的距离

        double index=0;

        double last=0;

        while(true) {

            last=index*index*index;

            if(last>input) {

                index-=distance;

                distance/=10;

            }

            if(distance<0.001) {

                break;

            }

            index+=distance;

        }

        double result=(int)((index+0.05)*10)/10.0;

        if(flag) {

            result=0-result;

        }

        System.out.println(result);

    }

}

全部评论

相关推荐

01-14 12:34
门头沟学院 C++
牛马人的牛马人生:太暖心了啊 配环境是真烦
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务