用概率法求PI
觉得思路很新颖,就搬上来了。#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main()
{
double x,y;
srand(time(0));
int n,count=0;
printf("请输入总数:");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
x=(double)rand()/RAND_MAX;
y=(double)rand()/RAND_MAX;
if(x*x+y*y<=1)
{
count++;
}
}
printf("PI的数值是:%lf",4.0*count/n);
}
[此贴子已经被作者于2018-9-23 14:53编辑过]