男女出生比例程序模拟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;
}
标签:rand,int,c++,--,出生,long,female,include,模拟 来源: https://blog.csdn.net/qq_42641977/article/details/121944799