我写的二分总是有点奇怪每次我都是枚举mid
,输出mid
,我看很多人会输出r
,我也不晓得。。
反正看代码吧,我也很少写double
是不是有精度问题也请指出
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;
int step;
double mid,x1,x2,l,r,y,a,b,c,d;
double f (double x)
{
return a*x*x*x+b*x*x+c*x+d;
}
int main()
{
scanf ("%lf %lf %lf %lf", &a, &b, &c, &d);
for (int i=-100;i<=99;i++)
{
l=i;
r=i+1;
x1 = f(l);
x2 = f(r);
if (x1==0)
{
printf ("%.2lf ", l);
step+=1;
continue;
}
if (x1*x2<0)
{
while (r-l>=0.001)
{
mid = (l+r)/2;
y = f(mid);
if (y==0)
{
printf ("%.2lf ", mid);
step+=1;
}
if (mid>0) l=mid+0.01;
else r=mid-0.01;
}
}
if (step==3)
{
break;
}
}
if (step!=3) printf ("100.00\n");
else printf ("\n");
return 0;
}
求求各位大佬了