其实就是写个板子,写一下这个公式的具体实现,可以做 hdu 1724 Ellipse、haut oj 1261地狱飞龙 练下手
具体代码:(代码中是求y=a*x+b在[L,R]上的积分)
#include <bits/stdc++.h>
using namespace std;
const double eps=1e-7;
double a,b,l,r;
int t;
double F(double x) // F[x]
{
return a*x+b;
}
double simpson(double a,double b)
{
double c=a+(b-a)/2;
return (F(a)+4*F(c)+F(b))*(b-a)/6;
}
double asr(double a,double b,double eps,double A)
{
double c=a+(b-a)/2;
double L=simpson(a,c),R=simpson(c,b);
if(fabs(L+R-A)<=15*eps) return L+R+(L+R-A)/15.0;
return asr(a,c,eps/2,L)+asr(c,b,eps/2,R);
}
double asr(double a,double b,double eps)
{
return asr(a,b,eps,simpson(a,b));
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf%lf%lf",&a,&b,&l,&r);
printf("%.3f\n", asr(l,r,eps));
}
return 0;
}