integral.cpp
来自「采用蒙特卡罗法计算pi的近似值」· C++ 代码 · 共 32 行
CPP
32 行
#include <stdio.h>
#include <iostream>
#include <time.h>
using namespace std;
void main(int argc, char* argv[])
{
double t,x,y,z=0;
int i,j=0,k;
cout<<"Please input the number of times(integer):";
cin>>t;
while(t-int(t))
{
cout<<endl<<"Please input an integer:";
cin>>t;
}
srand( (unsigned)time( NULL ) );//srand()函数产生一个以当前时间开始的随机种子
for(k=9;k>=0;k--)
{
z=0;
for (i=1;i<=int(t);i++)
{
x=(1.0*rand())/RAND_MAX;//x在0~1之间
y=(2.0*rand())/RAND_MAX;//y在0~2之间
z+=x*x+y*y;
}
cout<<2*z/t<<endl;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?