题目分析:
- 根的范围在-100到100之间
- 根与根之间差的绝对值>=1,我们可以用二分判断答案所在最小区间1,求解答案区间在[i,i + 1] (-100<=i<100)
- 实数范围用浮点数二分
代码如下:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
#define inf 0x3f3f3f3f
#define eps 1e-6
#define db double
#define ll long long
#define mm(a,x) memset(a,x,sizeof a)
#define pii pair<int,int>
#define pb push_back
#define el endl
#define debug(x) cerr<<#x<<" = "<<x
#define fgx cerr<<"-------------------------"<<endl
#define shutio ios::sync_with_stdio(false),cin.tie(0)
#define mk make_pair
#define lowbit(x) (x) & (-x)
#define fi first
#define se second
db a,b,c,d;
db fc(db x) {
return a * x * x * x + b * x * x + c * x + d;
}
int main() {
shutio;
cin >> a >> b >> c >> d;
int s = 0;
for(int i = -100; i < 100; i ++ ) {
db l = i;
db r = i + 1;
db fa = fc(l);
db fb = fc(r);
if(!fa) {
printf("%.2f ",l);
s ++;
continue;
}
if(fa * fb < 0) {
while(r - l > eps) {
db mid = (l + r) / 2;
if(fc(mid) * fc(r) >= 0) r = mid;
else l = mid;
}
s ++;
printf("%.2f ",l);
}
if(s == 3) break;
}
return 0;
}