Files
python/TangDou/LuoGuBook/P1024.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

38 lines
953 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
//本题目是浮点数二分的板子题
const double eps = 1e-8;
double a, B, c, d;
//方程
double f(double x) { return a * x * x * x + b * x * x + c * x + d; }
int main() {
//输入
cin >> a >> b >> c >> d;
double x, y, mid;
for (int i = -100; i <= 100; i++) {
x = i, y = i + 1; //左右边界,循环尝试每一个可能的边界因为题目中说了每两个解之间的差是不小1的
//边界
if (f(x) == 0) {
printf("%.2f ", x); //如果符合条件则直接输出
continue;
}
//如果其中有解就开始二分
if (f(x) * f(y) < 0) { //题目中有提示
//精度要求
while (y - x >= eps) {
mid = (x + y) / 2;
if (f(x) * f(mid) <= 0)
y = mid; //判断解在哪个区间
else
x = mid;
}
printf("%.2f ", x);
}
}
return 0;
}