题解 | #求解立方根#

求解立方根

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-16 22:33
杉川机器人 嵌入式工程师 18.0k*13.0, 年终奖1~9个月浮动
点赞 评论 收藏
分享
kl_我是东山啊:《相关公司:阿里巴巴》
投递阿里巴巴等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务