#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define M_PI 3.14159265
int isInsideCircle(double x, double y, double centerX, double centerY) {
return (pow(x
- centerX
, 2) + pow(y
- centerY
, 2)) < 1.0; }
int main() {
double x, y;
int count, n;
double areaEstimate, intersectionArea;
intersectionArea = 0.0;
for (n = 10; n <= 100000; n *= 5) {
count = 0;
areaEstimate = 0.0;
for (int m = 0; m < n; m++) {
x
= (double)rand() / RAND_MAX
; y
= (double)rand() / RAND_MAX
;
if (isInsideCircle(x, y, 0, 0) &&
isInsideCircle(x, y, 1, 0) &&
isInsideCircle(x, y, 0, 1) &&
isInsideCircle(x, y, 1, 1)) {
areaEstimate += 1.0;
}
count++;
}
areaEstimate /= count;
double theoreticalArea = M_PI - 4;
printf("%d\t%f\n", n
, fabs(areaEstimate
- theoreticalArea
)); }
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPG1hdGguaD4KCiNkZWZpbmUgTV9QSSAzLjE0MTU5MjY1CgppbnQgaXNJbnNpZGVDaXJjbGUoZG91YmxlIHgsIGRvdWJsZSB5LCBkb3VibGUgY2VudGVyWCwgZG91YmxlIGNlbnRlclkpIHsKICAgIHJldHVybiAocG93KHggLSBjZW50ZXJYLCAyKSArIHBvdyh5IC0gY2VudGVyWSwgMikpIDwgMS4wOwp9CgppbnQgbWFpbigpIHsKICAgIGRvdWJsZSB4LCB5OwogICAgaW50IGNvdW50LCBuOwogICAgZG91YmxlIGFyZWFFc3RpbWF0ZSwgaW50ZXJzZWN0aW9uQXJlYTsKICAgIGludGVyc2VjdGlvbkFyZWEgPSAwLjA7CgogICAgcHJpbnRmKCJuXHRFcnJvclxuIik7CgogICAgZm9yIChuID0gMTA7IG4gPD0gMTAwMDAwOyBuICo9IDUpIHsKICAgICAgICBjb3VudCA9IDA7CiAgICAgICAgYXJlYUVzdGltYXRlID0gMC4wOwoKICAgICAgICBmb3IgKGludCBtID0gMDsgbSA8IG47IG0rKykgewogICAgICAgICAgICB4ID0gKGRvdWJsZSlyYW5kKCkgLyBSQU5EX01BWDsKICAgICAgICAgICAgeSA9IChkb3VibGUpcmFuZCgpIC8gUkFORF9NQVg7CgogICAgICAgICAgICBpZiAoaXNJbnNpZGVDaXJjbGUoeCwgeSwgMCwgMCkgJiYgCiAgICAgICAgICAgICAgICBpc0luc2lkZUNpcmNsZSh4LCB5LCAxLCAwKSAmJiAKICAgICAgICAgICAgICAgIGlzSW5zaWRlQ2lyY2xlKHgsIHksIDAsIDEpICYmIAogICAgICAgICAgICAgICAgaXNJbnNpZGVDaXJjbGUoeCwgeSwgMSwgMSkpIHsKICAgICAgICAgICAgICAgIGFyZWFFc3RpbWF0ZSArPSAxLjA7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgY291bnQrKzsKICAgICAgICB9CgogICAgICAgIGFyZWFFc3RpbWF0ZSAvPSBjb3VudDsKICAgICAgICBkb3VibGUgdGhlb3JldGljYWxBcmVhID0gTV9QSSAtIDQ7CiAgICAgICAgcHJpbnRmKCIlZFx0JWZcbiIsIG4sIGZhYnMoYXJlYUVzdGltYXRlIC0gdGhlb3JldGljYWxBcmVhKSk7CiAgICB9CgogICAgcmV0dXJuIDA7Cn0=