题解 | #求解立方根#
求解立方根
https://www.nowcoder.com/practice/caf35ae421194a1090c22fe223357dca
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { double a = in.nextDouble(); double b = a/2; for(;;){ if(a==Double.parseDouble(String.format("%.3f",Math.pow(b,3)))) break; if((a>0&&Math.pow(b,3)>a)||(a<0&&Math.pow(b,3)<a)){ b=b/2; }else if((a>0&&Math.pow(b,3)<a)||(a<0&&Math.pow(b,3)>a)){ b=(b*2+b)/2; } } System.out.println(String.format("%.1f",b)); } } }