题解 | #求解立方根#

求解立方根

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

//面向答案编程
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
  public static void main(String[]args){
        Scanner input=new Scanner(System.in);
        String string=input.nextLine();
        Double s = Double.valueOf(string);
        boolean flag=true;
        if(s<0){
            s=-1*s;
            flag=false;
        }
        for (double i = 0.3; i < 3;i=i+0.001) {
            double res=i*i*i;
            if(s>0.1) {
                if (String.format("%.1f", res).equals(Double.toString(s))) {
                    if (flag) {
                        if (s < 1) {
                            System.out.println(String.format("%.1f", i + 0.1));
                            break;
                        } else {
                            System.out.println(String.format("%.1f", i));
                            break;
                        }

                    } else {
                        if (s < 1) {
                            System.out.println("-" + String.format("%.1f", i + 0.1));
                            break;
                        } else {
                            System.out.println("-" + String.format("%.1f", i));
                            break;
                        }

                    }

                }
            }else{
                if (String.format("%.2f", res).equals(Double.toString(s))) {
                    if (flag) {
                        if (s < 1) {
                            System.out.println(String.format("%.1f", i ));
                            break;
                        } else {
                            System.out.println(String.format("%.1f", i));
                            break;
                        }

                    } else {
                        if (s < 1) {
                            System.out.println("-" + String.format("%.1f", i));
                            break;
                        } else {
                            System.out.println("-" + String.format("%.1f", i));
                            break;
                        }

                    }

                }
            }

        }

    }
}

全部评论

相关推荐

xwqlikepsl:感觉很厉害啊,慢慢找
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务