思路:二分答案
题目约定该方程存在三个不同实根(根的范围在 −100−100 至 100100 之间),且根与根之差的绝对值 ≥1;
那么就可以在这个区间里枚举 一个数和它后面那个比他大1的数 ,在这两个数之间二分答案,即可解出答案。
注意:在循环判断时,应该r-l>=0.001,就能保证答案是三位小数,然后再进位,输出两位小数的情况。
#include<bits/stdc++.h>
#define endl "\n";
using namespace std;
typedef long long ll;
const int N=1e6+5;
typedef pair<int,int> PII;
const int INF=0x3f3f3f3f;
typedef struct node{
}node;
void IOS(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
}
//试试upper_bound(),lower_bound();
double a,b,c,d;
double fun(double x){
return x*x*x*a+b*x*x+c*x+d;
}
double rfun(double l,double r){
while(r-l>=0.0001){
double mid=(l+r)/2;
if(fun(l)*fun(mid)<=0){
r=mid;
}
else{
l=mid;
}
}
return l;
}
void solve(){
cin>>a>>b>>c>>d;
for(int i=-100;i<=100;++i){
double em=i,em2=i+1;
if(fun(em)==0){
cout<<fixed<<setprecision(2)<<em<<" ";
}
else if(fun(em)*fun(em2)<0){
cout<<fixed<<setprecision(2)<<rfun(em,em2)<<" ";
}
}
}
int main(){
IOS();
solve();
return 0;
}