题解 | #求解立方根#

求解立方根

https://www.nowcoder.com/practice/caf35ae421194a1090c22fe223357dca

// mark一下

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) { 
            double x = Double.valueOf(in.nextLine());
            double left = Math.min(x, -1);
            double right = Math.max(x, 1);
            double ans = (double) right;
            while (right - left > 0.001) {
                double mid = (left + right) / 2;
                double midThree = (double) mid * mid * mid;
                if (midThree > x) {
                    right = mid;
                } else if (midThree < x) {
                    left = mid;
                } else {
                    ans = mid;
                }
            }
            System.out.printf("%.1f", right);
        }
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
巧克力1:双选会不如教室宣讲会
点赞 评论 收藏
分享
Noob1024:一笔传三代,人走笔还在
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务