笨人的🧠实在是想不出来哪里有什么问题(拜托大家看看了,欧内该...):
#include <iostream>
#include <cmath>
double a,b,c,d;
using namespace std;
bool check1(double res) {
if (a * res * res * res + b * res * res + c * res + d == 0) return true;//判是否为解
return false;
}
bool check2(double x1,double x2) {
if (x1 < x2 && a * x1 * x1 * x1 + b * x1 * x1 + c * x1 + d < a * x2 * x2 * x2 + b * x2 * x2 + c * x2 + d) return true;//判解的区间
return false;
}
int main() {
cin >> a >> b >> c >> d;
int k = 0;
double l = -100,r = 100;
while (r - l >= 1e-4){
double mid = (l + r)/2;
if (check1(mid)) {//mid是一个解
printf ("%.2lf",mid);
if (check2(mid + 1.0,r)) l = mid + 1.0;
else if (check2(l,mid - 1.0)) r = mid - 1.0;
else break;
}
else if (check2(mid,r)) l = mid;//mid不是解,解在mid右边
else if (check2(l,mid)) r = mid;//mid不是解,解在mid左边
else break;
}
}