|
カテゴリ:IT
#include <iostream>
#include <random> using namespace std; int main(){ const int nst = 40; // 1クラスの人数 const int nrep = 100000; // 繰り返し回数 const int nday = 365; // 1年の日数 int hit[nday]; int count; random_device rdev; // ランダムシード mt19937 engine(rdev()); // MT法乱数生成器にランダムシードを設定 uniform_int_distribution<> dist(0, nday-1); // 一様整数乱数発生器 count = 0; for (int kk = 0; kk < nrep; kk++) { // 初期化 for (int ii = 0; ii < nday; ii++) { hit[ii] = 0; } // 1クラス分の誕生日データ作成 for (int ii = 0; ii < nst; ii++) { int select = dist(engine); hit[select]++; } // ワンペアチェック for (int ii = 0; ii < nday; ii++) { if (hit[ii] >= 2) { count++; break; } } } // 結果出力 double pc = 100.0 * double(count) / double(nrep); cout << pc << " %" << " (" << count << "/" << nrep << ")" << endl; return 0; }
お気に入りの記事を「いいね!」で応援しよう
Last updated
2017.06.17 12:54:00
[IT] カテゴリの最新記事
|