C/C++の宿題を片付けます 86代目
■ このスレッドは過去ログ倉庫に格納されています
0332デフォルトの名無しさん
2007/04/29(日) 01:14:03#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define Nx 1
#define Ny 2
int main()
{
double x, y, s;
int n, count_in = 0, count_out = 0;
srand((unsigned int)time(NULL));
for(n = 0; n < 10000; n++){
x = ((double)rand()/RAND_MAX)*Nx + 1;
y = ((double)rand()/RAND_MAX)*Ny + 1;
if(x >= 1 && y >= 1 && pow(x-1, 2) + pow(y-2, 2) <= 1)
count_in++;
else
count_out++;
}
s = (double)count_in/(count_in + count_out)*Nx*Ny;
printf("s = %f\n", s);
printf("pi = %f\n", 2*s);
return 0;
}
■ このスレッドは過去ログ倉庫に格納されています