#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define M_PI 3.14159265
double true_area() {
return 9.0;
}
int main() {
double x, y, s, r = 1.0;
int n, m;
double area_true = true_area();
for (n = 10; n <= 100000; n *= 10) {
s = 0.0;
for (m = 0; m < n; m++) {
x
= (double)rand() / RAND_MAX
* 2.0 - 1.0; y
= (double)rand() / RAND_MAX
* 2.0 - 1.0;
if ((x * x + y * y <= r * r) &&
(x - 1.0) * (x - 1.0) + y * y <= r * r &&
(x * x + (y - 1.0) * (y - 1.0)) <= r * r &&
((x - 1.0) * (x - 1.0) + (y - 1.0) * (y - 1.0)) <= r * r) {
s += 1.0;
}
}
double area_estimated = (s / n) * 4.0;
double error
= fabs(area_estimated
- area_true
);
printf("n = %d, 誤差 = %f\n", n
, error
); }
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPG1hdGguaD4KCiNkZWZpbmUgTV9QSSAzLjE0MTU5MjY1Cgpkb3VibGUgdHJ1ZV9hcmVhKCkgewogICAgcmV0dXJuIDkuMDsKfQoKaW50IG1haW4oKSB7CiAgICBkb3VibGUgeCwgeSwgcywgciA9IDEuMDsKICAgIGludCBuLCBtOwogICAgZG91YmxlIGFyZWFfdHJ1ZSA9IHRydWVfYXJlYSgpOwoKICAgIGZvciAobiA9IDEwOyBuIDw9IDEwMDAwMDsgbiAqPSAxMCkgewogICAgICAgIHMgPSAwLjA7CgogICAgICAgIGZvciAobSA9IDA7IG0gPCBuOyBtKyspIHsKICAgICAgICAgICAgeCA9IChkb3VibGUpcmFuZCgpIC8gUkFORF9NQVggKiAyLjAgLSAxLjA7CiAgICAgICAgICAgIHkgPSAoZG91YmxlKXJhbmQoKSAvIFJBTkRfTUFYICogMi4wIC0gMS4wOwoKICAgICAgICAgICAgaWYgKCh4ICogeCArIHkgKiB5IDw9IHIgKiByKSAmJiAKICAgICAgICAgICAgICAgICh4IC0gMS4wKSAqICh4IC0gMS4wKSArIHkgKiB5IDw9IHIgKiByICYmIAogICAgICAgICAgICAgICAgKHggKiB4ICsgKHkgLSAxLjApICogKHkgLSAxLjApKSA8PSByICogciAmJgogICAgICAgICAgICAgICAgKCh4IC0gMS4wKSAqICh4IC0gMS4wKSArICh5IC0gMS4wKSAqICh5IC0gMS4wKSkgPD0gciAqIHIpIHsKICAgICAgICAgICAgICAgIHMgKz0gMS4wOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICBkb3VibGUgYXJlYV9lc3RpbWF0ZWQgPSAocyAvIG4pICogNC4wOwogICAgICAgIGRvdWJsZSBlcnJvciA9IGZhYnMoYXJlYV9lc3RpbWF0ZWQgLSBhcmVhX3RydWUpOwoKICAgICAgICBwcmludGYoIm4gPSAlZCwg6Kqk5beuID0gJWZcbiIsIG4sIGVycm9yKTsKICAgIH0KCiAgICByZXR1cm4gMDsKfQo=