代码如下,似乎没有实现动态存储
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
srand((unsigned)time(NULL));
int* month = new int[12]();
for (int i = 0; i < 300; i++)
{
int a = rand() % 12 + 1;//a为1-12月
switch (a)
{
case 1: month[0]++; break;
case 2: month[1]++; break;
case 3: month[2]++; break;
case 4: month[3]++; break;
case 5: month[4]++; break;
case 6: month[5]++; break;
case 7: month[6]++; break;
case 8: month[7]++; break;
case 9: month[8]++; break;
case 10: month[9]++; break;
case 11: month[10]++; break;
case 12: month[11]++; break;
}
}
for (int i = 0; i < 12; i++)
cout << "第" << i + 1 << "月出生率为:" << month[i] * 1.0 / 3 << "%" << endl;
delete[]month;
return 0;
}