3074. 自适应辛普森积分

题目链接

3074. 自适应辛普森积分

给定两个整数 a,b,请计算如下积分:

abSin(x)xdx

输入格式

共一行,包含两个实数 a,b

输出格式

输出一个实数,表示结果。

结果保留 6 位小数。

数据范围

1a<b100

输入样例:

1.0 2.0

输出样例:

0.659330

解题思路

自适应辛普森积分

辛普森积分主要用来求解任意一个连续函数在一段区间的积分(面积),类似于微积分的定义,即将区间分为很多微小的小段,将区间的高假定为某个值,然后用矩形的面积取代该段不规则的形状的面积,而辛普森积分则是用一个二次函数近似小段的曲线,其用到一个求解一次二次函数某个区间 [l,r] 的面积公式:S=(rl)×f(l)+4×f(mid)+f(r)6

而自适应辛普森积分则是先用面积公式求出 [l,r] 内的面积 S,再分别求出左、右半边的面积 Sl,Sr,然后拿 SSl+Sr 进行比较,如果其误差在允许的范围内说明该值可取,否则递归处理左右半边

自适应辛普森积分主要处理一些精度要求不高的问题,而且要求函数低阶

  • 时间复杂度:()

代码

// Problem: 自适应辛普森积分 // Contest: AcWing // URL: https://www.acwing.com/problem/content/3077/ // Memory Limit: 64 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> //#define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } const double eps=1e-12; double l,r; double f(double x) { return sin(x)/x; } double simpson(double l,double r) { double mid=(l+r)/2; return (f(l)+f(mid)*4+f(r))/6*(r-l); } double asr(double l,double r,double s) { double mid=(l+r)/2; double L=simpson(l,mid),R=simpson(mid,r); if(fabs(s-L-R)<eps)return L+R; return asr(l,mid,L)+asr(mid,r,R); } int main() { scanf("%lf%lf",&l,&r); printf("%.6lf",asr(l,r,simpson(l,r))); return 0; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/16919555.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(99)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示