#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define TRIALS 100000 // 試行回数
#define COINS 3 // コインの枚数
int main() {
int histogram[COINS + 1] = {0}; // 表の枚数ごとのカウントを保存する配列
int heads;
// 乱数の種を設定
// 試行を行う
for (int i = 0; i < TRIALS; i++) {
heads = 0; // 表の枚数をリセット
// コインを3回投げる
for (int j = 0; j < COINS; j++) {
if (rand() % 2 == 0) { // 0なら表、1なら裏とする heads++;
}
}
// 表の枚数をヒストグラムにカウント
histogram[heads]++;
}
// 結果の表示
for (int i = 0; i <= COINS; i++) {
printf("%d\t\t%d\n", i
, histogram
[i
]); }
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHRpbWUuaD4KCiNkZWZpbmUgVFJJQUxTIDEwMDAwMCAgLy8g6Kmm6KGM5Zue5pWwCiNkZWZpbmUgQ09JTlMgMyAgICAgICAgLy8g44Kz44Kk44Oz44Gu5p6a5pWwCgppbnQgbWFpbigpIHsKICAgIGludCBoaXN0b2dyYW1bQ09JTlMgKyAxXSA9IHswfTsgIC8vIOihqOOBruaemuaVsOOBlOOBqOOBruOCq+OCpuODs+ODiOOCkuS/neWtmOOBmeOCi+mFjeWIlwogICAgaW50IGhlYWRzOwoKICAgIC8vIOS5seaVsOOBrueoruOCkuioreWumgogICAgc3JhbmQoKHVuc2lnbmVkIGludCl0aW1lKE5VTEwpKTsKCiAgICAvLyDoqabooYzjgpLooYzjgYYKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgVFJJQUxTOyBpKyspIHsKICAgICAgICBoZWFkcyA9IDA7ICAvLyDooajjga7mnprmlbDjgpLjg6rjgrvjg4Pjg4gKCiAgICAgICAgLy8g44Kz44Kk44Oz44KSM+WbnuaKleOBkuOCiwogICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgQ09JTlM7IGorKykgewogICAgICAgICAgICBpZiAocmFuZCgpICUgMiA9PSAwKSB7ICAvLyAw44Gq44KJ6KGo44CBMeOBquOCieijj+OBqOOBmeOCiwogICAgICAgICAgICAgICAgaGVhZHMrKzsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgLy8g6KGo44Gu5p6a5pWw44KS44OS44K544OI44Kw44Op44Og44Gr44Kr44Km44Oz44OICiAgICAgICAgaGlzdG9ncmFtW2hlYWRzXSsrOwogICAgfQoKICAgIC8vIOe1kOaenOOBruihqOekugogICAgcHJpbnRmKCLooajjga7mnprmlbBcdOippuihjOWbnuaVsFxuIik7CiAgICBmb3IgKGludCBpID0gMDsgaSA8PSBDT0lOUzsgaSsrKSB7CiAgICAgICAgcHJpbnRmKCIlZFx0XHQlZFxuIiwgaSwgaGlzdG9ncmFtW2ldKTsKICAgIH0KCiAgICByZXR1cm4gMDsKfQo=