HUST 1360 Solve the integration 【自适应simpson积分】

本文介绍了一个复杂的积分问题求解过程,使用Simpson模板进行数值积分,并通过具体代码实现了解题思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值