HUST 1360 Solve the integration 自适应simpson积分模板题

来源:互联网 发布:孙叔敖之知翻译 编辑:程序博客网 时间:2024/06/18 15:25

1360 - Solve the integration

Time Limit: 1s Memory Limit: 256MB

Submissions: 203 Solved: 53
Description
Wincat is a clever boy, he has just had a math class and has just learned about Integration, and just think is too easy to learn. Then he say to He’s math teacher Peter “it is too easy for me, could your give more hard lesson?” say with Disdain.”Ok, If you can solve this problem I give to your ,then I will teach your more hard lesson”. Then Peter turn around to the blackboard and write down a Equation like below:
Wincat is confused by this question, you are one of good friend of him ,could your help him?
Input
The input contains multiple test cases, each case give you a float number x (0.0<=x<=100.0) .
Output
Output the result of that equation, Round the numbers in the output to 4 digits after decimal point.
Sample Input
0.2
Sample Output
0.0200
Hint

Source
中国地质大学(武汉)第七届ACM程序设计大赛暨华中地区部属高校ACM邀请赛


训练时发现自己根本解不出来这个积分(数学渣渣QAQ)。赛后才知道这题可以用模板去做。。。那么以后碰到这样的题就方便多了。


simpson模板:


模板理解:a和b分别代表所求积分的上下限。eps的值一般为 : 1e-6

                    同时还要加上一个函数double F(double x);这个函数就是你要求的函数


上代码:

#include<cstdio>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>using namespace std;const double eps=1e-6;///eps的值double F(double t){    return t/sqrt(t*t*t + 1.0);///对应题目所需求解的方程}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(){    double x;    while(cin>>x)    {        printf("%.4lf\n",asr(0,x,eps));///传入上下限和eps    }    return 0;}

可以分为几个部分来看

1: F()函数部分

double F(double t){    return t/sqrt(t*t*t + 1.0);///对应题目所需求解的方程}

2:模板部分

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));}
虽说是套了模板,但是还是要努力理解啊QAQ

0 0
原创粉丝点击