#include<iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
int havesamebirthday(int stu[], int n);
int main()
{
int stu[367], n;
float x, p;
srand((unsigned)time(NULL));
for (n = 10; n < 81; n++)
{
float sum = 0;
for (int i = 0; i < 10000; i++)
{
for (int j = 0; j < n; j++)
{
stu[j] = rand() % 366 + 1;
}
x = havesamebirthday(stu, n);
sum += x;
}
p = sum / 10000.0;
cout << "n=" << n << "人的概率为" << "p=" << p << endl;
}
system("pause");
return 0;
}
int havesamebirthday(int stu[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (stu[i] == stu[j])
{
return 1;
}
}
}
return 0;
}
统计同一天生.
最新推荐文章于 2022-06-06 23:41:16 发布