給定多項式方程 8x⁴ + 7x³ + 2x² + 3x + 6 = Y,要求在區間 [0, 100] 內求解 x,精度要求小數點後 4 位。若無解則輸出 "No solution!"。
定義函數 f(x) = 8x⁴ + 7x³ + 2x² + 3x + 6。其導函數 f'(x) = 32x³ + 21x² + 4x + 3,在 [0, 100] 上恒大於 0,故 f(x) 嚴格單調遞增。因此方程最多有一個解。
double f(double x) {
return 8*x*x*x*x + 7*x*x*x + 2*x*x + 3*x + 6;
// 或使用 Horner 規則:((8*x + 7)*x + 2)*x + 3)*x + 6
}
int main() {
int T; scanf("%d", &T);
while (T--) {
double Y; scanf("%lf", &Y);
double L = 0, R = 100;
double fL = f(L), fR = f(R);
if (Y < fL || Y > fR) {
puts("No solution!");
continue;
}
while (R - L > 1e-7) { // 精度略高於 1e-4
double mid = (L + R) / 2;
if (f(mid) > Y) R = mid;
else L = mid;
}
printf("%.4f\n", (L + R) / 2);
}
return 0;
}
double 保證精度