ch11_13.c

来自「C语言开发入门与编程实践 源码文件」· C语言 代码 · 共 29 行

C
29
字号
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define N 1000000

int main()
{
    float x, y;
    int i, sum = 0;
    long int seed;
    seed=time(NULL);/*以系统时间当做随机数种子*/ 
    srand(seed);
    
    for(i = 0; i < N; i++)
    {
        x = rand();
        y = rand();
        x /= 32767;
        y /= 32767;
        if((pow(x,2)+pow(y,2)) <= 1)
            sum++;/* 计算1/4圆内的点数 */ 
    }
    printf("PI = %.5f\n", (float)sum/N*4);/* 打印出PI值 */ 

    system("pause");
    return 0;
}

⌨️ 快捷键说明

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