German Collegiate Programming Contest 2013:E

数值计算:

这种积分的计算方法很好,学习一下!

代码:

 1 #include <iostream>
 2 #include <cmath>
 3 using namespace std;
 4 const double eps = 10e-5;
 5 
 6 double func(double a, double b, double x)
 7 {
 8     double r = a * exp(- x*x) + b * sqrt(x);
 9     return r*r;
10 }
11 
12 double integrate(double a, double b, double h)
13 {
14     unsigned long steps = 1, it = 1;
15     double V = h*(func(a,b,0)+func(a,b,h))/2.0;
16     double Vold;
17     do
18     {
19         double tmp = 0;
20         steps *= 2;
21         for (unsigned long i = 1; i < steps; i += 2)
22         {
23             tmp += func(a, b, (h * i) / steps);
24         }
25         Vold = V;
26         V = (V / 2.0) + ((tmp * h) / steps);
27     }
28     while (fabs(V - Vold) > eps);
29     return V;
30 }
31 
32 int main()
33 {
34     double V, a, b, h;
35     double e_min = 2e100;
36     int i_min = 0;
37     int N;
38     cin >> V >> N;
39     V /= 3.14159265358979;
40     for (int i = 0; i < N; i++)
41     {
42         cin >> a >> b >> h;
43         double e = fabs(V -integrate(a, b, h));
44         if (e < e_min)
45         {
46             e_min = e;
47             i_min = i;
48         }
49     }
50     cout << i_min << endl;
51     return 0;
52 }
View Code

posted @ 2013-10-20 16:56  Yours1103  阅读(202)  评论(0编辑  收藏  举报