男女出生比例程序模拟c++
模拟:男女出生概率相等的情况下,家庭偏爱生男孩
结果:出生率只和出生概率有关,和生娃策略无关
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
long long male,female,n,t;
const int N=1e7;
int main()
{
n=N;
srand( (unsigned)time( NULL ) );
rand();
while(n--)
{
t=3;
while(t--)
{
int tmp=rand();
if(tmp%2==0){
male++;
break;
}
else female++;
}
}
cout<<"male:"<<male<<endl;
cout<<"female:"<<female<<endl;
return 0;
}