8_3计算圆周率.cpp
来自「下楼问题」· C++ 代码 · 共 20 行
CPP
20 行
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
# include<math.h>
void main()
{
long k=0,c=0,d=0;
float pai=0,x=0,y=0;
srand((unsigned int)time(NULL));
for (k=0;k<10000000;k++)
{
d=d+1;
x=(float)rand()/32767;
y=(float)rand()/32767;
if (sqrt(x*x+y*y)<=1)
c=c+1;
}
pai=4.0f*c/d;
cout<<"pai的近似值为"<<pai<<endl;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?