P1024 一元三次方程求解

时间:2022-09-16 23:00:40

P1024 一元三次方程求解


#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
double a,b,c,d;
double f(double data)
{
return a*data*data*data+b*data*data+c*data+d;
}
void bit_sreach(double l,double r)
{
while(l+0.001<r)
{
double mid=(l+r)/2;
if(f(mid)==0.0)//一个坑点
{
printf("%.2lf",mid);
break;
}
if(f(mid)*f(l)<0)
r=mid;
if(f(mid)*f(r)<0)
l=mid;
}
printf("%.2lf ",l);
}
int main()
{
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
for(double i=-100;i<=100;i++)
{
if(f(i)==0.0)//注意
{
printf("%.2lf ",i);
}
if(f(double(i))*f(double(i+1))<0)
bit_sreach(i,i+1);
}
return 0;
}