CRUELL2 - Cô giáo dạy toán, phần II

Tác giả: happyboy99x

Ngôn ngữ: C++

#include<cstdio>
#include<cmath>

/* F(x) = p[0] + p[1] * x + p[2] * x^2 + ... + p[d] * x^d */
int d; double p[12];
const double EPS = 1e-6;

double f(double x) {
	double res = 0, x2 = 1;
	for(int i = 0; i <= d; ++i) res += x2 * p[i], x2 *= x;
	return res;
}

int main() {
	scanf("%d", &d); for(int i = 0; i <= d; ++i) scanf("%lf", p+i);
	double l = -1e6, h = 1e6;
	if(f(l) > f(h)) for(int i = 0; i <= d; ++i) p[i] = -p[i];
	while(fabs(h - l) > EPS) {
		double x = (l + h) / 2, y = f(x);
		if(y > 0) h = x; else l = x;
	}
	printf("%d\n", int((l+h)/2*1000));
	return 0;
}

Download