pi.cpp

来自「利用布丰投针的思想」· C++ 代码 · 共 32 行

CPP
32
字号
// PI.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdlib.h"
#include "time.h"
#include "math.h"
#define TIME 65536
#define EDGE 512

int main()
{

	int k=0;
	double a;
	double pi;
    srand((unsigned)time(NULL));

	for(long i=0;i<TIME;i++)
	{
		int x=rand()%EDGE;
	    int y=rand()%EDGE;
		a=sqrt(x*x+y*y);
		if(a<EDGE)k++;
	}
	pi=4*(double)k/TIME;
	printf("PI=%f\n",pi);
    scanf("PI=%f\n",pi);
	return 0;
}

⌨️ 快捷键说明

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