题解 | #求解立方根#
求解立方根
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));
}
}
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));
}
}