麻了,为啥第二个根一直错
查看原帖
麻了,为啥第二个根一直错
859183
ReverSouth楼主2023/2/11 17:36
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

double a,b,c,d,l=-101,r;
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);
    int flag=0;
    while(l<101)
    {
        l+=1;r=l+1;
        if(!f(l)){printf("%.2lf ",l);flag++;}
        if(f(l)*f(r)<0)
        {
            while(r-l>1e-8)
            {
                double m=(r+l)/2;
                if(f(m)>0)r=m;
                else l=m;
            }
            printf("%.2lf ",l);
            cout<<abs(f(l))<<endl;
            flag++;
        }
        if(flag==3)break;
    }
    return 0;
}
2023/2/11 17:36
加载中...