⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 integral.cpp

📁 采用蒙特卡罗法计算pi的近似值
💻 CPP
字号:
#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -