题解 | #求解立方根#
求解立方根
https://www.nowcoder.com/practice/caf35ae421194a1090c22fe223357dca
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double input = in.nextDouble();
double num = input >= 0 ? input : -input;
int top = 0, bottom = 0;
while (top * top * top < num) {
top++;
}
bottom = top - 1;
int p1 = 1;
while(p1 <= 10) {
double n = bottom + ((double)p1) / 10;
if (n * n * n > num) break;
p1++;
}
p1 -= 1;
int p2 = 1;
double f1 = bottom + ((double)p1) / 10;
while(p2 <= 10) {
double n = f1 + ((double)p2) / 100;
if (n * n * n > num) break;
p2++;
}
p2 -= 1;
if (p2 >= 5) {
p1 = p1 + 1;
}
System.out.println((input > 0 ? "" : "-") + (bottom + ((double)p1) / 10));
}
}
