#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM_TRIALS 100000 // 試行回数
int main() {
int histogram[4] = {0, 0, 0, 0}; // 表の枚数ごとのカウント (0枚, 1枚, 2枚, 3枚)
int i, j, heads;
// 乱数の初期化
// 試行を繰り返す
for (i = 0; i < NUM_TRIALS; i++) {
heads = 0;
// コインを3回投げる
for (j = 0; j < 3; j++) {
if (rand() % 2 == 0) { // 0なら表 (1/2の確率) heads++;
}
}
// 表の枚数をヒストグラムに記録
histogram[heads]++;
}
// 結果を表示
printf("コインを3枚投げた試行 %d 回の結果:\n", NUM_TRIALS
); for (i = 0; i < 4; i++) {
printf("%d\t\t%d\n", i
, histogram
[i
]); }
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHRpbWUuaD4KCiNkZWZpbmUgTlVNX1RSSUFMUyAxMDAwMDAgLy8g6Kmm6KGM5Zue5pWwCgppbnQgbWFpbigpIHsKICAgIGludCBoaXN0b2dyYW1bNF0gPSB7MCwgMCwgMCwgMH07IC8vIOihqOOBruaemuaVsOOBlOOBqOOBruOCq+OCpuODs+ODiCAoMOaemiwgMeaemiwgMuaemiwgM+aemikKICAgIGludCBpLCBqLCBoZWFkczsKCiAgICAvLyDkubHmlbDjga7liJ3mnJ/ljJYKICAgIHNyYW5kKCh1bnNpZ25lZCBpbnQpdGltZShOVUxMKSk7CgogICAgLy8g6Kmm6KGM44KS57mw44KK6L+U44GZCiAgICBmb3IgKGkgPSAwOyBpIDwgTlVNX1RSSUFMUzsgaSsrKSB7CiAgICAgICAgaGVhZHMgPSAwOwoKICAgICAgICAvLyDjgrPjgqTjg7PjgpIz5Zue5oqV44GS44KLCiAgICAgICAgZm9yIChqID0gMDsgaiA8IDM7IGorKykgewogICAgICAgICAgICBpZiAocmFuZCgpICUgMiA9PSAwKSB7IC8vIDDjgarjgonooaggKDEvMuOBrueiuueOhykKICAgICAgICAgICAgICAgIGhlYWRzKys7CiAgICAgICAgICAgIH0KICAgICAgICB9CgogICAgICAgIC8vIOihqOOBruaemuaVsOOCkuODkuOCueODiOOCsOODqeODoOOBq+iomOmMsgogICAgICAgIGhpc3RvZ3JhbVtoZWFkc10rKzsKICAgIH0KCiAgICAvLyDntZDmnpzjgpLooajnpLoKICAgIHByaW50Zigi44Kz44Kk44Oz44KSM+aemuaKleOBkuOBn+ippuihjCAlZCDlm57jga7ntZDmnpw6XG4iLCBOVU1fVFJJQUxTKTsKICAgIHByaW50Zigi6KGo44Gu5p6a5pWwXHTpoLvluqZcbiIpOwogICAgZm9yIChpID0gMDsgaSA8IDQ7IGkrKykgewogICAgICAgIHByaW50ZigiJWRcdFx0JWRcbiIsIGksIGhpc3RvZ3JhbVtpXSk7CiAgICB9CgogICAgcmV0dXJuIDA7Cn0K