1.全局变量可以让函数少参数
2.二分 分治
3.注意double 如mid int不行
4.double类型比大小不能用==
#include <bits/stdc++.h>
using namespace std;
int i = -100, x;
double a, b, c, d;
double fu(double x)
{
return a * x * x * x + b * x * x + c * x + d;
}
int main()
{
double m, n, l, r;
cin >> a >> b >> c >> d;
for (i = -100; i < 100; i++)
{
int flag = 0;
l = i;
r = i + 1;
m = fu(l);
n = fu(r);
if (m == 0)
{
printf("%.2lf ", l);
flag++;
}
if (m * n < 0)
{
while (r - l >= 0.001)
{
double mid = (l + r) / 2;
if (fu(mid) * fu(r) <= 0)
l = mid;
else
r = mid;
}
printf("%.2lf ", r);
flag++;
}
if (flag == 3)
break;
}
return 0;
}
别人写的暴力枚举
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
double a,b,c,d;
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
for(double i=-100;i<=100;i+=0.001)
{
double j=i+0.001;
double y1=a*i*i*i+b*i*i+c*i+d;
double y2=a*j*j*j+b*j*j+c*j+d;
if(y1>=0&&y2<=0||y1<=0&&y2>=0)
{
double x=(i+j)/2;
printf("%.2lf ",x);
}
}
}