50分求改 qwq
查看原帖
50分求改 qwq
1339308
cannotlauchdemo_exe楼主2024/9/4 12:58
#include<bits/stdc++.h>

using namespace std;

double a, b, c, d;

double f(double x) {
	return a * x * x * x + b * x * x + c * x + d;
}

int ans = 0;

vector<double> root;

void bi_search(double x, double y) {
	if (ans == 3) {
		return;
	}
	if (f(x) == 0) {
		root.push_back(x);
		ans++;
		return;
	}
	if (f(x) * f(y) < 0) {
		double mid = (x + y) / 2;
		if (f(x) * f(mid) < 0) {
			bi_search(x, mid);
		}
		else {
			bi_search(mid, y);
		}
	}
}


void search()
{
	for (double i = -100; i < 100; i++) {
		if (f(i) == 0) {
			ans++;
			root.push_back(i);
		}
		else {
			bi_search(i, i + 1);
		}
	}
}

int main()
{
	std::cin >> a >> b >> c >> d;

	search();

	for (const auto & r : root) {
		std::cout << fixed << setprecision(2) << r << " ";
	}

	return 0;

}
2024/9/4 12:58
加载中...