题解 | #求解立方根#
求解立方根
https://www.nowcoder.com/practice/caf35ae421194a1090c22fe223357dca
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextDouble()) { // 注意 while 处理多个 case
double input=in.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{
bottom=mid;
}
mid=bottom+ (top-bottom)/2;
mul=mid*mid*mid;
}
if(input<0){
mid=-mid;
}
System.out.println(String.format("%.1f",mid));
}
}
}
