为啥2,4过不了,大佬救命
查看原帖
为啥2,4过不了,大佬救命
854450
fufengyiye楼主2024/1/29 14:22
#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

double a, b, c, d;
double f(double x)
{
	return a * x * x * x + b * x * x + c * x + d;
}
int main()
{
	double l, r, m, x1, x2;
	int cnt = 0;
	cin >> a >> b >> c >> d;
	for (double i = -100.0; i < 100.0; i++)
	{
		l = i;
		r = i + 1;
		x1 = f(l);
		x2 = f(r);
		if (!x1)
		{
			printf("%.2lf ", i);
			cnt++;
			continue;
		}
		if (x1 * x2 < 0)
		{
			while (r - l >= 0.001)
			{
				m = (l + r) / 2;
				if (f(m) * f(r) <= 0)
					l = m;
				else
					r = m;
			}
			printf("%.2lf  ", r);
			cnt++;
		}
		if (cnt == 3)
			break;
	}

	return 0;
}

调了个把小时了。我哭死

2024/1/29 14:22
加载中...