题解 | #求解立方根#

求解立方根

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

import java.util.Scanner;
public class Main {
   public static void main(String[] args){
       Scanner scanner = new Scanner(System.in);
       double input = scanner.nextDouble();
       double num = input>0?input:-input;
       double bottom = 0;
       double top = 0;
       while(top*top*top< num){
           top++;
       }
       bottom = top-1;
       double mid = bottom + (top - bottom)/2;
       double mul = mid*mid*mid;
       while(top - bottom > 0.1){
           if(mul>num){
               top = mid;
           }else if(mul<num){
               bottom = mid;
           }
           mid = bottom + (top - bottom)/2;
           mul = mid*mid*mid;
       }
       if(input<0){
           mid = -mid;
       }
       System.out.println(String.format("%.1f",mid));
   }     
}
全部评论

相关推荐

头像 会员标识
02-14 15:34
门头沟学院 Java
Java抽象带篮子:专业技能怎么写可以看看我发的帖子
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务