辛普森积分法 - 维基百科,自由的百科全书

Simpson's rule - Wikipedia, the free encyclopedia

  利用这个公式,用二分的方法来计算积分。

1071 ( The area )

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath> using namespace std; const double EPS = 1e-;
double A, B, C, P, Q; template<class T> T sqr(T x) { return x * x;}
inline double cal(double x) { return A * sqr(x) + (B - P) * x + C - Q;}
inline double sps(double l, double r) { return (cal(l) + cal(r) + * cal((l + r) / )) / * (r - l);} double work(double l, double r) {
//cout << l << ' ' << r << endl;
double ans = sps(l, r), m = (l + r) / ;
if (fabs(ans - sps(l, m) - sps(m, r)) < EPS) return ans;
else return work(l, m) + work(m, r);
} int main() {
int T;
double l, r;
double x[], y[];
cin >> T;
while (T--) {
for (int i = ; i < ; i++) cin >> x[i] >> y[i];
double p[], q[], d[];
for (int i = ; i < ; i++) p[i] = sqr(x[i]) - sqr(x[i + ]), q[i] = x[i] - x[i + ], d[i] = y[i] - y[i + ];
A = (q[] * d[] - q[] * d[]) / (p[] * q[] - p[] * q[]);
B = (p[] * d[] - p[] * d[]) / (p[] * q[] - p[] * q[]);
C = y[] - B * x[] - A * sqr(x[]);
//cout << A << ' ' << B << ' ' << C << endl;
P = (y[] - y[]) / (x[] - x[]);
Q = y[] - P * x[];
//cout << P << ' ' << Q << endl;
printf("%.2f\n", work(x[], x[]));
}
return ;
}

1724 ( Ellipse )

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath> using namespace std; const double EPS = 1e-;
double A, B; template<class T> T sqr(T x) { return x * x;}
inline double cal(double x) { return * B * sqrt( - sqr(x) / sqr(A));}
inline double sps(double l, double r) { return (cal(l) + cal(r) + * cal((l + r) / )) / * (r - l);} double work(double l, double r) {
//cout << l << ' ' << r << endl;
double ans = sps(l, r), m = (l + r) / ;
if (fabs(ans - sps(l, m) - sps(m, r)) < EPS) return ans;
else return work(l, m) + work(m, r);
} int main() {
int T;
double l, r;
cin >> T;
while (T-- && cin >> A >> B >> l >> r) printf("%.3f\n", work(l, r));
return ;
}

  之后还有题会继续更新。

UPD:

  就是因为见过这题,所以才学这个公式的。1y~

ACM-ICPC Live Archive

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath> using namespace std; double coe[][];
const double EPS = 1e-; int k;
double cal(double x, double *c) {
double ret = c[];
for (int i = ; i <= k; i++) ret *= x, ret += c[i];
return ret;
} inline double cal(double x, double *p, double *q) { return cal(x, p) / cal(x, q);}
inline double cal(double x, double y, double *p, double *q) { return max(cal(x, p, q) - y, 0.0);}
inline double simpson(double y, double l, double r, double *p, double *q) { return (cal(l, y, p, q) + cal(r, y, p, q) + * cal((l + r) / , y, p, q)) * (r - l) / ;} inline double getpart(double y, double l, double r, double *p, double *q) {
double sum = simpson(y, l, r, p, q);
//cout << l << ' ' << r << ' ' << sum << endl;
if (fabs(sum - simpson(y, l, (l + r) / , p, q) - simpson(y, (l + r) / , r, p, q)) < EPS) return sum;
return getpart(y, l, (l + r) / , p, q) + getpart(y, (l + r) / , r, p, q);
} inline double getarea(double y, double l, double r, double *p, double *q) {
double ret = , d = (r - l) / ;
for (int i = ; i < ; i++) {
ret += getpart(y, l + d * i, l + d * (i + ), p, q);
}
return ret;
} double dc2(double l, double r, double a, double w) {
double m;
while (r - l > EPS) {
m = (l + r) / 2.0;
//cout << m << ' ' << getarea(m, 0, w, coe[0], coe[1]) - getarea(m, 0, w, coe[2], coe[3]) << endl;
if (getarea(m, , w, coe[], coe[]) - getarea(m, , w, coe[], coe[]) > a) l = m;
else r = m;
}
return l;
} int main() {
//freopen("in", "r", stdin);
//freopen("out", "w", stdout);
double w, d, a;
while (cin >> w >> d >> a >> k) {
for (int i = ; i < ; i++) for (int j = ; j <= k; j++) cin >> coe[i][j];
for (int i = ; i < ; i++) reverse(coe[i], coe[i] + k + );
//cout << getarea(-5.51389, 0, w, coe[0], coe[1]) - getarea(-5.51389, 0, w, coe[2], coe[3]) << endl;
//cout << cal(3, coe[0], coe[1]) << endl;
printf("%.5f\n", -dc2(-d, , a, w));
}
return ;
}

——written by Lyon

最新文章

  1. 一个过滤特殊字符的JS
  2. welcome to learn prgram
  3. struts2:非表单标签
  4. java ee 中文乱码的问题
  5. python在线文档
  6. iOS:自定义工具栏、导航栏、标签栏
  7. Java程序员的成长之路
  8. 002.Ceph安装部署
  9. VUE2.0 饿了吗视频学习笔记(一):VUE示例data.json
  10. [UE4]传送门:场景切换
  11. #define 和typedef
  12. DFS服务待书写
  13. 关于InfiniBand几个基本知识点解释
  14. linux入门系列
  15. HBase的Scan
  16. php 抽象 继承 多态
  17. Spring MVC同一方法返回JSON/XML格式
  18. vue2 简单的留言板
  19. ZooKeeper安装和配置(转)
  20. mysql中日期操作

热门文章

  1. [jnhs]使用netbeans生成的webapp发布到tomcat是需要改名字的,不然就是404Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
  2. Python3入门机器学习 经典算法与应用
  3. 洛谷P2723 丑数 Humble Numbers [2017年 6月计划 数论07]
  4. hdu 1251 统计难题(trie树入门)
  5. Floyd算法模板--详解
  6. eclipse修改中文注释的字体(亲测有用!)
  7. spring拦截器Interceptor
  8. 使用cnpm真的会有诡异的Bug
  9. OSGi Capabilities
  10. 自学FPAG笔记之 &quot; top_down “