生日悖论C++代码

匿名 (未验证) 提交于 2019-12-02 23:48:02
#include <ctime> #include <iostream> #include <algorithm>  using namespace std;  class birthday { public:     birthday(int n = 10000, int m = 50) : n(n), m(m) {}      void test()     {         srand((unsigned)time(nullptr));         int cnt = 0;         for (int i = 0; i < n; ++i)         {             bool *p = new bool[365];             fill(p, p + 365, false);             for (int j = 0; j < m; ++i)             {                 auto x = rand() % 365;                 if (p[x])                     break;                 else                 {                     p[x] = true;                     cnt++;                 }             }             delete[] p;         }         cout << cnt * 1.0 / n << endl;     }  private:     int n; //实验次数     int m; //人数 };   int main() {     birthday a;     a.test();      return 0; }

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!