显然,我实际上是想创建一个在许多试验(5000)中随机分配生日的数组。然后假设每次至少有 2 个生日为 2 - 50 人计数,并将结果除以 5,000 以获得近似概率。我相信我的循环搞砸了,希望得到一些反馈。不是代码,我想确切地了解出了什么问题以及我是如何搞砸的。
int main()
{
const int trials(5000);
double total;
int count(0), birthdays[49];
srand(time(NULL));
for (int i = 2; i <= 50; i++)
{
for (int k = 0; k < trials; k++)
{
fillUp(birthdays, 49);
for (int j = i + 1; j <= 50; j++)
{
if (birthdays[i] == birthdays[j])
{
count += 1;
}
}
}
total = count / 5000.0;
cout << "For " << i << " the probability is " << total << endl;
}
return 0;
}
void fillUp(int birthdays [], int size)
{
for (int i = 0; i < size; i++)
{
birthdays[i] = rand() % 365 + 1;
}
}
输出:
For 2 the probability is 0.1286
For 3 the probability is 0.2604
...
...
For 49 the probability is 3.9424
For 50 the probability is 3.9424
任何帮助将非常感激。