📄 calcpi.cpp
字号:
// CalcPi.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char* argv[])
{
const int throws = 10000000;
SYSTEMTIME now;
SYSTEMTIME end;
GetSystemTime (&now);
srand ((unsigned) time (NULL));
int Inside = 0;
for (int i = 0; i < throws; ++i)
{
double cx = (double) rand() / (double) RAND_MAX;
double cy = (double) rand() / (double) RAND_MAX;
double distance = sqrt ((cx * cx) + (cy * cy));
if (distance < 1.0)
++Inside;
}
double pi = 4 * (double) Inside / (double) throws;
GetSystemTime (&end);
int msStart = 1000 * 60 * now.wMinute + 1000 * now.wSecond + now.wMilliseconds;
int msEnd = 1000 * 60 * end.wMinute + 1000 * end.wSecond + end.wMilliseconds;
int milliseconds = msEnd - msStart;
printf ("pi = %6f\n", pi);
printf ("Elapsed time = %d milliseconds\n", milliseconds);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -