?? pi.cpp
字號:
#include "iostream.h"
#include "math.h"
#include "stdlib.h"
void main()
{
int count=0,total=8000;
double x,y,z,pi,a,s;
cout<<"輸入試驗次數: "<<'\n';
cin>>total;
for(int i=0;i<total;i++)
{
a=(rand()*1.0)/RAND_MAX;
do
{
x=(rand()*1.0)/RAND_MAX;
y=(rand()*1.0)/RAND_MAX;
}
while(x*x+y*y>=1);
s=x/sqrt(x*x+y*y);
z=a+s;
if(z>1||z<0)
count++;
if(i%1000==1)
{
pi=2.0*i/count;
cout<<pi<<'\n';
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -