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

📄 montecarlo.cpp

📁 刚才传错了……这个才是采用蒙特卡罗法计算pi的近似值的源程序。方法是随机产生二维0~1之间的均匀分布数据点
💻 CPP
字号:
#include <stdio.h> 
#include <iostream> 
#include <time.h> 
using namespace std; 

void main(int argc, char* argv[]) 
{
 double t,x,y;
 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--)
	{j=0;
	 for (i=1;i<=int(t);i++) 
		{
		 x=(1.0*rand())/RAND_MAX;//x、y的范围在0~1之间
		 y=(1.0*rand())/RAND_MAX;
		 if(x*x+y*y<1)j++;
		 }
	 cout<<4*j/t<<endl;
	}	 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -