-
如果一个数num>1,那么这个数的立方根一定在1~num之间。
-
如果一个数num<-1,那么这个数的立方根一定在num~-1
-
如果一个数-1<num<1,那么这个数的立方根一定在-1~1之间
如num = 2.7,可以设置左边界为min(-1,2.7) = -1 右边界 max(1,2.7) = 2.7,所以left = -1,right = 2.7
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
double num = in.nextDouble();
double left, right, mid = 0.00;
left = Math.min(-1.0, num);
right = Math.max(1.0, num);
while (right - left > 0.001) {
mid = (left + right) / 2;
if (mid * mid * mid > num)
right = mid;
else if (mid * mid * mid < num)
left = mid;
else
System.out.printf("%.1f", mid);
}
System.out.printf("%.1f", right);
}
}
}