HDU1724 Ellipse(数值积分)

补一下一些小盲区,譬如simpson这种数值积分的方法虽然一直知道,但是从未实现过,做一道例题存一个模板。

#pragma warning(disable:4996)

#include<iostream>

#include<cstring>

#include<string>

#include<cstdio>

#include<vector>

#include<cmath>

using namespace std;



double a, b, l, r;



double sqr(double x){

	return x*x;

}



double f(double x){

	return b / a*sqrt(sqr(a) - sqr(x));

}



double simpson(double l, double r)

{

	return (f(l) + 4 * f((l + r) / 2) + f(r))*(r - l) / 6;

}



double simpson(double l, double r, double all, double eps){

	double m = (l + r) / 2;

	double L = simpson(l, m), R = simpson(m, r);

	if (fabs(L + R - all) <= 15 * eps) return L + R + (L + R - all) / 15;

	return simpson(l, m, L, eps / 2.0) + simpson(m, r, R, eps / 2.0);

}



double simpson(double l, double r, double eps){

	return simpson(l, r, simpson(l, r), eps);

}



int main()

{

	int T; cin >> T;

	while (T--){

		cin >> a >> b >> l >> r;

		printf("%.3lf\n", 2*simpson(l, r, 1e-4));

	}

	return 0;

}

 

你可能感兴趣的:(HDU)