📄 pufens.cpp
字号:
#include "iostream.h"
#include "stdlib.h"
#include "stdio.h"
#include "math.h"
#include<time.h>
#define pi 3.141
double CreRand()//产生随机数函数;
{
double y;
y=1.0*rand()/RAND_MAX;//产生(0,1)之间的随机数;
return y;//cout<<y<<endl;
}
void main()
{
double a=20,l=20;
double x,theta;
double y1,y2;
int m=0;
double p=0,q=0;
double pai;
srand((unsigned)time(NULL));//将系统时间作为产生随机数的种子;
m=0;
for(int j=0;j<10000;j++)
{
y1=CreRand();
//cout<<y1<<endl;
y2=CreRand();
//cout<<y2<<endl;
x=a*y1;
theta=pi*y2;
if (x<=l*sin(theta))
m=m+1;
}
pai=2*l/(a*m/10000);
cout<<"所求pi近似值为:"<<pai<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -