[NOIP2001 提高组] 一元三次方程求解

以0.01精度在[-100,100]枚举根。

#include<iostream>
#include<iomanip>
int main()
{
    double a,b,c,d;
    std::cin>>a>>b>>c>>d;
    for(double x=-100.0;x<=100.0;x+=0.01)
    {
        double f=a*x*x*x+b*x*x+c*x+d;
        if(f>-0.01&&f<0.01)//因为精度丢失的问题,这里不能写f==0
            std::cout<<std::setprecision(2)<<std::fixed<<x<<" ";
    }
}

 

posted @ 2022-08-21 19:47  nichengmeibeiyong  阅读(39)  评论(0编辑  收藏  举报