
数值计算:
这种积分的计算方法很好,学习一下!
代码:


#include <iostream>
#include <cmath>
using namespace std;
const double eps = 10e-; double func(double a, double b, double x)
{
double r = a * exp(- x*x) + b * sqrt(x);
return r*r;
} double integrate(double a, double b, double h)
{
unsigned long steps = , it = ;
double V = h*(func(a,b,)+func(a,b,h))/2.0;
double Vold;
do
{
double tmp = ;
steps *= ;
for (unsigned long i = ; i < steps; i += )
{
tmp += func(a, b, (h * i) / steps);
}
Vold = V;
V = (V / 2.0) + ((tmp * h) / steps);
}
while (fabs(V - Vold) > eps);
return V;
} int main()
{
double V, a, b, h;
double e_min = 2e100;
int i_min = ;
int N;
cin >> V >> N;
V /= 3.14159265358979;
for (int i = ; i < N; i++)
{
cin >> a >> b >> h;
double e = fabs(V -integrate(a, b, h));
if (e < e_min)
{
e_min = e;
i_min = i;
}
}
cout << i_min << endl;
return ;
}