题解 | #求解立方根#
求解立方根
https://www.nowcoder.com/practice/caf35ae421194a1090c22fe223357dca
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.text.DateFormat; import java.text.DecimalFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; import java.util.*; import java.util.stream.IntStream; import java.util.stream.Stream; import static java.util.Arrays.*; import static java.util.stream.Stream.*; public class Main { public static void main(String[] args) throws IOException { testTh(); } private static void testTh() throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String str; StringBuilder sb = new StringBuilder(); DecimalFormat df = new DecimalFormat("#0.0"); while ((str = bf.readLine()) != null) { double parseDouble = Double.parseDouble(str); int flag = parseDouble >= 0 ? 1 : 0; double min = Double.MAX_VALUE; double tempo = 0; if (flag == 1) { for (double i = 0.1; i <= 2.8 ; i = i + 0.1) { double res = i * i * i; double v = parseDouble - Double.parseDouble(df.format(res)); if(v<0)v=-v; if (v < min) { min = v; tempo = i; } } } if (flag == 0) { for (double i = -0.1; i >= -2.8 ; i = i - 0.1) { double res = i * i * i; double v = parseDouble - Double.parseDouble(df.format(res)); if(v<0)v=-v; if (v < min) { min = v; tempo = i; } } } System.out.println(df.format(tempo)); } } }