题解 | #求解立方根#
求解立方根
https://www.nowcoder.com/practice/caf35ae421194a1090c22fe223357dca
//穷举单位0.1
#include <math.h>
#include <stdio.h>
int main() {
double a, b;
scanf("%lf", &a);
if (a > 0) {
b = 0.1;
while (pow(b, 3) < a) {
b += 0.1;
}
if ((pow(b, 3) - a) > (a - pow(b - 0.1, 3)) ) {
printf("%0.1f", b - 0.1);
} else {
printf("%0.1f", b);
}
} else if (a < 0) {
b = -0.1;
while (pow(b, 3) > a) {
b -= 0.1;
}
if ((pow(b, 3) - a) < (a - pow(b - 0.1, 3)) ) {
printf("%0.1f", b + 0.1);
} else {
printf("%0.1f", b);
}
} else {
printf("0");
}
return 0;
}