fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define M_PI 3.14159265
  6.  
  7. double true_area = 2.828;
  8.  
  9. int main() {
  10. double a, b, s, x1, x2, y1, y2, q1, q2;
  11. int count, m, n;
  12.  
  13. x1 = 1.0; y1 = 0.0;
  14. x2 = 0.0; y2 = 1.0;
  15. count = 0;
  16.  
  17. for (n = 10; n <= 100000; n = n * 5) {
  18. count = 0;
  19. s = 0.0;
  20.  
  21. for (m = 0; m < n; m++) {
  22. a = (double)rand() / RAND_MAX;
  23. b = (double)rand() / RAND_MAX;
  24.  
  25. q1 = (a - x1) * (a - x1) + (b - y1) * (b - y1);
  26. q2 = (a - x2) * (a - x2) + (b - y2) * (b - y2);
  27.  
  28. if (q1 < 1.0 && q2 < 1.0) {
  29. s += 1.0;
  30. }
  31. }
  32.  
  33. s /= n;
  34. printf("%d\t%f\n", n, fabs(s - true_area));
  35. }
  36.  
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0s 5284KB
stdin
 
stdout
10	2.228000
50	2.328000
250	2.188000
1250	2.249600
6250	2.257920
31250	2.256832