题解 | #求解立方根#

求解立方根

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 input = in.nextDouble();
        double num = input >= 0 ? input : -input;

        int top = 0, bottom = 0;
        while (top * top * top < num) {
            top++;
        }
        bottom = top - 1;

        int p1 = 1;
        while(p1 <= 10) {
            double n = bottom + ((double)p1) / 10;
            if (n * n * n > num) break;
            p1++;
        }
        p1 -= 1;

        int p2 = 1;
        double f1 = bottom + ((double)p1) / 10;
        while(p2 <= 10) {
            double n = f1 + ((double)p2) / 100;
            if (n * n * n > num) break;
            p2++;
        }
        p2 -= 1;
        if (p2 >= 5) {
            p1 = p1 + 1;
        }

        System.out.println((input > 0 ? "" : "-") + (bottom + ((double)p1) / 10));
    }
}

全部评论

相关推荐

卡卡罗特ovo:说起云智我就来气,约好了一面,结果面试官没来,ssob上问hr也未读,我还是专门请了半天假在家面试,恶心死了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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