算法练习10

来源:互联网 发布:音箱分频器设计软件 编辑:程序博客网 时间:2024/04/29 18:54

求π近似值2

#include <stdio.h>
#include <stdlib.h>
#define N 30000
int main()
{
   float x,y;
   int c = 0;
   int d = 0;
   while(c++ <= N)
   {
       x = rand()%101+0;
       y = rand()%101+0;
       if(x*x + y*y <= 10000)
       {
           d++;
       }
   }

  
   printf("PI:%f\n",(float)4*d/N);
    return 0;
}

0 0