题解 | #求解立方根#

求解立方根

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));
   }     
}
全部评论

相关推荐

每晚夜里独自颤抖:你cet6就cet6,cet4就cet4,你写个cet证书等是什么意思。专业技能快赶上项目行数,你做的这2个项目哪里能提现你有这么多技能呢
点赞 评论 收藏
分享
05-09 14:45
门头沟学院 Java
点赞 评论 收藏
分享
如题,他是要劝退我了吗
椛鸣:根据你的时间 来给你安排任务 如果你时间长 可能会参与到一些长期的项目 时间短 那就只能做点零工
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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