#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM_TRIALS 100000 // 試行回数
#define NUM_COINS 3 // コインの枚数
int main() {
int histogram[NUM_COINS + 1] = {0}; // 表が出た枚数をカウントする配列
int i, j, heads;
// 乱数シードの初期化
// 試行を繰り返す
for (i = 0; i < NUM_TRIALS; i++) {
heads = 0; // 表のカウントを初期化
// 3枚のコインを投げる
for (j = 0; j < NUM_COINS; j++) {
if (rand() % 2 == 0) { // 0: 表, 1: 裏 heads++;
}
}
// 表の枚数をヒストグラムに記録
histogram[heads]++;
}
// 結果を出力
for (i = 0; i <= NUM_COINS; i++) {
printf("%d\t\t%d\n", i
, histogram
[i
]); }
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHRpbWUuaD4KCiNkZWZpbmUgTlVNX1RSSUFMUyAxMDAwMDAgLy8g6Kmm6KGM5Zue5pWwCiNkZWZpbmUgTlVNX0NPSU5TIDMgICAgICAgLy8g44Kz44Kk44Oz44Gu5p6a5pWwCgppbnQgbWFpbigpIHsKICAgIGludCBoaXN0b2dyYW1bTlVNX0NPSU5TICsgMV0gPSB7MH07IC8vIOihqOOBjOWHuuOBn+aemuaVsOOCkuOCq+OCpuODs+ODiOOBmeOCi+mFjeWIlwogICAgaW50IGksIGosIGhlYWRzOwoKICAgIC8vIOS5seaVsOOCt+ODvOODieOBruWIneacn+WMlgogICAgc3JhbmQodGltZShOVUxMKSk7CgogICAgLy8g6Kmm6KGM44KS57mw44KK6L+U44GZCiAgICBmb3IgKGkgPSAwOyBpIDwgTlVNX1RSSUFMUzsgaSsrKSB7CiAgICAgICAgaGVhZHMgPSAwOyAvLyDooajjga7jgqvjgqbjg7Pjg4jjgpLliJ3mnJ/ljJYKCiAgICAgICAgLy8gM+aemuOBruOCs+OCpOODs+OCkuaKleOBkuOCiwogICAgICAgIGZvciAoaiA9IDA7IGogPCBOVU1fQ09JTlM7IGorKykgewogICAgICAgICAgICBpZiAocmFuZCgpICUgMiA9PSAwKSB7IC8vIDA6IOihqCwgMTog6KOPCiAgICAgICAgICAgICAgICBoZWFkcysrOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICAvLyDooajjga7mnprmlbDjgpLjg5Ljgrnjg4jjgrDjg6njg6DjgavoqJjpjLIKICAgICAgICBoaXN0b2dyYW1baGVhZHNdKys7CiAgICB9CgogICAgLy8g57WQ5p6c44KS5Ye65YqbCiAgICBwcmludGYoIuihqOOBruaemuaVsFx05Ye654++5Zue5pWwXG4iKTsKICAgIGZvciAoaSA9IDA7IGkgPD0gTlVNX0NPSU5TOyBpKyspIHsKICAgICAgICBwcmludGYoIiVkXHRcdCVkXG4iLCBpLCBoaXN0b2dyYW1baV0pOwogICAgfQoKICAgIHJldHVybiAwOwp9Cg==