题解 | #求解立方根#
import java.util.Scanner;
public class CubeRoot {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double num = scanner.nextDouble();
double nagNum = -num;
if(num<0){
num = -num;
}
int pointLeft = 0;
double pointRight1 = 0;
double pointRight2 = 0;
double val = 0;
for(int i=1 ;i<num;i++){
if((i*i*i) <=num && (i+1)*(i+1)*(i+1)>=num){
pointLeft=i;
}
}
for(int i=1;i<10;i++){
double right1=(double)i/10;
double temp = 0;
temp = pointLeft+right1;
if(temp*temp*temp<=num && (temp+0.1)*(temp+0.1)*(temp+0.1)>=num){
pointRight1=right1;
}
}
for(int i=1;i<10;i++){
double right2=(double)i/100;
double temp = 0;
temp = pointLeft+pointRight1+right2;
if(temp*temp*temp<=num && (temp+0.01)*(temp+0.01)*(temp+0.01)>=num){
pointRight2=right2;
}
}
if(pointRight2<0.05){
val=pointLeft+pointRight1;
}else {
int x = (int)((pointRight1+0.1)*100)/10;
pointRight1 = (double) x/10;
val = pointLeft+pointRight1;
}
if(nagNum>0){
val=-val;
}
System.out.println(val);
}
}
#华为笔试#华为笔试题 文章被收录于专栏
保证运行通过