https://www.nowcoder.com/practice/caf35ae421194a1090c22fe223357dca
强行二分,这是一种不好的解法,缝缝补补,又臭又长
import java.util.Scanner;
import java.math.BigDecimal;public class Main{public static void main(String[] args){Scanner sc=new Scanner(System.in);int flag=0;String str=sc.nextLine();String[] split = str.split("\\.");int prec=0;if (split.length==1)prec=0;else prec=split[1].length();double target=Double.valueOf(str);if(target<0){flag=-1;target=-target;}else flag=1;BigDecimal low=new BigDecimal("0.0");BigDecimal high=new BigDecimal(target);BigDecimal mid=new BigDecimal("0.0");if(target<=1)high=new BigDecimal(1.0);while (low.compareTo(high)<1){mid=(high.add(low)).divide(new BigDecimal("2.0")).setScale(2,BigDecimal.ROUND_DOWN);BigDecimal mid2=mid.multiply(mid.multiply(mid));double mid2Value=mid2.setScale(prec,BigDecimal.ROUND_HALF_UP).doubleValue();if (mid2Value==target){System.out.println(flag*(mid.setScale(1,BigDecimal.ROUND_HALF_UP).doubleValue()));return;}else if(mid2Value>target){high=mid.subtract(new BigDecimal("0.01"));}else{low=mid.add(new BigDecimal("0.01"));}}System.out.println(flag*mid.setScale(1,BigDecimal.ROUND_HALF_UP).doubleValue());}
}