求教为什么只有一个结果输出
查看原帖
求教为什么只有一个结果输出
486575
Owen214358881楼主2021/8/13 15:20
#include <iostream>
#include <iomanip>
using namespace std;
double a, b, c, d;
double fc(double x)
{
    return a * x * x * x + b * x * x + c * x + d;
}

int main()
{
    double m, n, x1, x2, c;
    int i, s = 0;
    cin >> a >> b >> c >> d;
    for (i = -100; i < 100; i++)
    {
        m = i;
        n = i + 1;
        x1 = fc(m);
        x2 = fc(n);
        if (x1 == 0)
        {
            cout << fixed << setprecision(2) << m << " ";
            s++;
        }
        else if (x1 * x2 < 0)
        {
            while (n - m >= 0.001)
            {
                c = (m + n) / 2;
                if (fc(c) * fc(n) <= 0)
                {
                    m = c;
                }
                else
                {
                    n = c;
                }
            }
            cout << fixed << setprecision(2) << n << " ";
            s++;
        }
        if (s == 3)
        {
            break;
        }
    }
    return 0;
}
2021/8/13 15:20
加载中...