wojiaocbj

simpson

Apr 5th, 2022
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.84 KB | None | 0 0
  1. /*
  2.  Author: 曹北健
  3.  Result: AC Submission_id: 4279111
  4.  Created at: Wed Apr 06 2022 07:37:06 GMT+0800 (China Standard Time)
  5.  Problem_id: 5486   Time: 14    Memory: 2672
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <math.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13. #pragma warning(disable:4996)
  14. typedef long long LL;
  15. typedef unsigned long long ULL;
  16. double lam(double t){
  17.     return exp(-((t + 0.5) * (t + 0.5)));
  18. }
  19. double simpson(int ndiv, double lb, double ub, double(*pfunc)(double)){
  20.     double h = (ub - lb) / ndiv;
  21.     double s = pfunc(lb) + pfunc(ub);
  22.     int i;
  23.     for(i = 1; i < ndiv; i += 2){
  24.         s += 4 * pfunc(lb + i * h);
  25.     }
  26.     for(i = 2; i < ndiv - 1; i += 2){
  27.         s += 2 * pfunc(lb + i * h);
  28.     }
  29.     return s * h / 3.;
  30. }
  31. int main(){
  32.     double ub;
  33.     scanf("%lf", &ub);
  34.     printf("%.6f\n", exp(-simpson(114514, 0, ub, lam)));
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment