题解 | #求解立方根#

求解立方根

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner fzhinput = new Scanner(System.in);
        double sz = fzhinput.nextDouble();
        double lfg;
        if (sz >= 1.0) {
            for (double i = 1.0; i <= sz; i+=0.1) {
                if(sz-i*i*i<=0.5 && sz%2==0){
                    String result = String.format ("%.1f", i);
                    System.out.println(result);
                    break;
                }
                else if(sz-i*i*i<=0.5 && sz%2!=0){         
                    String result = String.format ("%.1f", i);
                    System.out.println(result);
                    break;
                }
            }
        } else if (sz <= -1.0) {
            for (double i = -1.0; i >= sz; i-=0.1) {
                if(sz-i*i*i>=-0.5){
                    String result = String.format ("%.1f", i);
                    System.out.println(result);
                    break;
                }
            }

        } else if (sz>-1.0&&sz<0){
            for (double i = -0.9; i < 0;i+=0.1) {
                if(sz<=i*i*i-0.0005&&i<0){
                    String result = String.format ("%.1f", i);
                    System.out.println(result);
                    break;
                }
            }
        }
        else
        {
            for (double i = 0.1; i < 1;i+=0.1) {
                if(sz<=i*i*i+0.0005&&i>0){
                    String result = String.format ("%.1f", i);
                    System.out.println(result);
                    break;
                }
            }
        }

    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务