这是本人第一个cpp程序
//建议Windows运行
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int main()
{
int a = 1; //上限
int b = 0; //下限
int times = 1; //次数
int zhengfan = 1; //正反
int t = 1; //总次数
srand(time(0));
while (1)
{
int front = 0; //正面次数
int back = 0; //反面次数
t = 1;
cout << "请输入次数,输入503202005退出程序:" << endl;
cin >> times;
if (times == 503202005)
{
break;
}
double be_t = clock();//开始计时
while (t <= times)//循环抛硬币
{
t += 1;
zhengfan = rand() % (2) + 0;
if (zhengfan == 1)
{
front += 1;
}
else if (zhengfan == 0)
{
back += 1;
}
}
double fi_t = clock();//停止计时
double delta_t = (fi_t - be_t) / CLOCKS_PER_SEC;//计算时间单位毫秒
/*尚未解决的bug,显示概率为零
float pf = front * 100 / ( front + back );
float pb = back * 100 / ( front + back );
*/
double f = 100 * front;
double b = 100 * back;
double pf = f / t;
double pb = b / t;
cout << "正面次数:" << front << "概率:" << pf << "%" << endl;
cout << "反面次数:" << back << "概率:" << pb << "%" << endl;
cout << "用时:" << fixed << delta_t << "秒" << endl;
//DEBUG
//DEBUG
if (delta_t > 0)
{
cout << "速度:" << (front + back) / delta_t << "次/秒" << endl;
}
else
{
cout << "电脑太快了,计时太短无法计算QWQ." << endl;
}
}
}