提交时间:2024-05-19 20:42:57
运行 ID: 47712
#include <cstdio> const double EPS = 1e-6; const double ACC = 1e-2; inline bool is_zero(double x) { return x <= EPS && x >= -EPS; } double poly(double e[], double x); double solve(double e[], double left, double right, bool inc); int main() { double e[4]; scanf("%lf %lf %lf %lf", &e[3], &e[2], &e[1], &e[0]); double s[3]; int cnt = 0; double left, right, lvalue, rvalue; bool inc; for (int i = -100; i < 100; ++i) { left = (double)i; right = (double)(i+1); lvalue = poly(e, left); if (is_zero(lvalue)) { s[cnt++] = left; if (cnt == 3) break; continue; } rvalue = poly(e, right); if (is_zero(rvalue)) { s[cnt++] = right; if (cnt == 3) break; ++i; continue; } if (lvalue < 0 && rvalue > 0) inc = true; else if (lvalue > 0 && rvalue < 0) inc = false; else continue; s[cnt++] = solve(e, left, right, inc); if (cnt == 3) break; } if (cnt == 2) s[2] = 100.0; printf("%.2lf %.2lf %.2lf\n", s[0], s[1], s[2]); return 0; } double poly(double e[], double x) { double p = e[3]; for (int i = 2; i >= 0; --i) p = x * p + e[i]; return p; } double solve(double e[], double left, double right, bool inc) { double mid, mvalue; while (right - left > ACC) { mid = (left + right) / 2.0; mvalue = poly(e, mid); if (is_zero(mvalue)) return mid; if ((mvalue < 0 && inc) || (mvalue > 0 && !inc)) left = mid; else right = mid; } return (left + right) / 2.0; }