#include<iostream>
#include<cmath>
#include<ctime>
#include<cstdlib>
using namespace std;
class gailv {
public:
gailv()
{
cout << "输入点的数量:" << endl;
}
int montepi(int a)
{
srand(time(NULL)); //产生随机数种子
for(int i=1;i<a;i++)
{
x = (double)rand()/RAND_MAX; //x方向上产生0~1的随机数
y =(double)rand()/ RAND_MAX; //y方向上产生0~1的随机数
if (pow(x, 2) + pow(y, 2) <= 1) //x^2+y^2<=1; 阴影的区域
sum++; //记录点的个数
}
pi = (4.0 * sum) / (double)a; // sum表示了阴影区域面积/a表示了正方形区域的面积
return pi;
}
~gailv()
{
cout << "蒙特卡洛算法所求的pi的值:" << pi << endl;
}
int n;
double pi;
double x, y;
double sum;
};
void text()
{
gailv gl;
cin>>gl.n;
gl.sum = 0;
gl.montepi(gl.n);
}
int main()
{
text();
}