#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand(time(0));
double x, y, pi;
long int i, n, cnt = 0;
printf("Input>");
scanf("%ld", &n);
for (int i = 0; i <= n; i++) {
x = (double)rand() / RAND_MAX * 2.0;
y = (double)rand() / RAND_MAX * 2.0;
printf("x=%lf, y=%lf\n", x, y);
if ((x - 1.0) * (x - 1.0) + (y - 1.0) * (y - 1.0)
cnt++;
pi = 4.0 * cnt / i;
printf("cnt=%ld, n=%ld, pi=%lf\n", cnt, i, pi);
}
return 0;
}
用随机数计算π值
最新推荐文章于 2022-04-24 08:00:00 发布