#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
// 単位円の内部かどうかを判定する関数
int isInsideCircle(double x, double y, double centerX, double centerY) {
return ((x - centerX) * (x - centerX) + (y - centerY) * (y - centerY)) <= 1.0;
}
int main() {
int i, numPoints = 1000000; // 試行回数(生成するランダム点の数)
int insideCount = 0; // 重複領域に入った点の数
double x, y; // ランダムに生成される点の座標
double area; // 重複領域の近似面積
// 乱数の初期化
// モンテカルロ法で点をランダムに生成し、4つの円の重複領域に入る点をカウント
for (i = 0; i < numPoints; i++) {
x
= (double)rand() / RAND_MAX
; // 0から1までのランダムなx座標 y
= (double)rand() / RAND_MAX
; // 0から1までのランダムなy座標
// 4つの円に入っているか確認 (中心が (0,0), (1,0), (0,1), (1,1) の円)
if (isInsideCircle(x, y, 0, 0) &&
isInsideCircle(x, y, 1, 0) &&
isInsideCircle(x, y, 0, 1) &&
isInsideCircle(x, y, 1, 1)) {
insideCount++;
}
}
// 面積の近似:単位正方形内での点の割合に4倍して面積を求める
area = 4.0 * (double)insideCount / numPoints;
// 結果を出力
printf("近似した重複領域の面積: %lf\n", area
);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPG1hdGguaD4KI2luY2x1ZGUgPHRpbWUuaD4KCi8vIOWNmOS9jeWGhuOBruWGhemDqOOBi+OBqeOBhuOBi+OCkuWIpOWumuOBmeOCi+mWouaVsAppbnQgaXNJbnNpZGVDaXJjbGUoZG91YmxlIHgsIGRvdWJsZSB5LCBkb3VibGUgY2VudGVyWCwgZG91YmxlIGNlbnRlclkpIHsKICAgIHJldHVybiAoKHggLSBjZW50ZXJYKSAqICh4IC0gY2VudGVyWCkgKyAoeSAtIGNlbnRlclkpICogKHkgLSBjZW50ZXJZKSkgPD0gMS4wOwp9CgppbnQgbWFpbigpIHsKICAgIGludCBpLCBudW1Qb2ludHMgPSAxMDAwMDAwOyAvLyDoqabooYzlm57mlbDvvIjnlJ/miJDjgZnjgovjg6njg7Pjg4Djg6Dngrnjga7mlbDvvIkKICAgIGludCBpbnNpZGVDb3VudCA9IDA7IC8vIOmHjeikh+mgmOWfn+OBq+WFpeOBo+OBn+eCueOBruaVsAogICAgZG91YmxlIHgsIHk7IC8vIOODqeODs+ODgOODoOOBq+eUn+aIkOOBleOCjOOCi+eCueOBruW6p+aomQogICAgZG91YmxlIGFyZWE7IC8vIOmHjeikh+mgmOWfn+OBrui/keS8vOmdouepjQoKICAgIC8vIOS5seaVsOOBruWIneacn+WMlgogICAgc3JhbmQodGltZShOVUxMKSk7CgogICAgLy8g44Oi44Oz44OG44Kr44Or44Ot5rOV44Gn54K544KS44Op44Oz44OA44Og44Gr55Sf5oiQ44GX44CBNOOBpOOBruWGhuOBrumHjeikh+mgmOWfn+OBq+WFpeOCi+eCueOCkuOCq+OCpuODs+ODiAogICAgZm9yIChpID0gMDsgaSA8IG51bVBvaW50czsgaSsrKSB7CiAgICAgICAgeCA9IChkb3VibGUpcmFuZCgpIC8gUkFORF9NQVg7IC8vIDDjgYvjgokx44G+44Gn44Gu44Op44Oz44OA44Og44GqeOW6p+aomQogICAgICAgIHkgPSAoZG91YmxlKXJhbmQoKSAvIFJBTkRfTUFYOyAvLyAw44GL44KJMeOBvuOBp+OBruODqeODs+ODgOODoOOBqnnluqfmqJkKCiAgICAgICAgLy8gNOOBpOOBruWGhuOBq+WFpeOBo+OBpuOBhOOCi+OBi+eiuuiqjSAo5Lit5b+D44GMICgwLDApLCAoMSwwKSwgKDAsMSksICgxLDEpIOOBruWGhikKICAgICAgICBpZiAoaXNJbnNpZGVDaXJjbGUoeCwgeSwgMCwgMCkgJiYKICAgICAgICAgICAgaXNJbnNpZGVDaXJjbGUoeCwgeSwgMSwgMCkgJiYKICAgICAgICAgICAgaXNJbnNpZGVDaXJjbGUoeCwgeSwgMCwgMSkgJiYKICAgICAgICAgICAgaXNJbnNpZGVDaXJjbGUoeCwgeSwgMSwgMSkpIHsKICAgICAgICAgICAgaW5zaWRlQ291bnQrKzsKICAgICAgICB9CiAgICB9CgogICAgLy8g6Z2i56mN44Gu6L+R5Ly877ya5Y2Y5L2N5q2j5pa55b2i5YaF44Gn44Gu54K544Gu5Ymy5ZCI44GrNOWAjeOBl+OBpumdouepjeOCkuaxguOCgeOCiwogICAgYXJlYSA9IDQuMCAqIChkb3VibGUpaW5zaWRlQ291bnQgLyBudW1Qb2ludHM7CgogICAgLy8g57WQ5p6c44KS5Ye65YqbCiAgICBwcmludGYoIui/keS8vOOBl+OBn+mHjeikh+mgmOWfn+OBrumdouepjTogJWxmXG4iLCBhcmVhKTsKCiAgICByZXR1cm4gMDsKfQo=