wojiaocbj

random-integral

Apr 2nd, 2022
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.73 KB | None | 0 0
  1. /*
  2.  Author: 曹北健
  3.  Result: AC Submission_id: 4269402
  4.  Created at: Sat Apr 02 2022 19:34:52 GMT+0800 (China Standard Time)
  5.  Problem_id: 5486   Time: 24    Memory: 2740
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <math.h>
  10. double f(double t){
  11.     return exp(-((t + 0.5) * (t + 0.5)));
  12. }
  13. double calcrnd(int testcnt, double lbx, double ubx, double lby, double uby){
  14.     int cnt = 0, i;
  15.     double x, y;
  16.     for(i = 1; i <= testcnt; i++){
  17.         x = ((rand() % 32768) / 32768.0) * (ubx - lbx) + lbx;
  18.         y = ((rand() % 32768) / 32768.0) * (uby - lby) + lby;
  19.         if(y < f(x))cnt++;
  20.     }
  21.     return (double)cnt / (double)testcnt * (ubx - lbx) * (uby - lby);
  22. }
  23. int main(){
  24.     double ub;
  25.     scanf("%lf", &ub);
  26.     printf("%.6f\n", exp(-calcrnd(114514, 0, ub, 0, 1)));
  27.     return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment