#include #include #include using namespace std; /* Calcul de la proportion d'une surface recouverte par des pieces lancees */ const int N_pieces = 10, N_points = 10000, N_fois = 100; const double rayon = 0.1; int main(){ int i, j, k, c; double xx, yy, d2, r, rr; double *x, *y; srand48(time(NULL)); x = (double*)malloc(N_pieces * sizeof(double)); y = (double*)malloc(N_pieces * sizeof(double)); rr = 0.; for (i = 0; i < N_fois; i++){ for (k = 0; k < N_pieces; k++){ x[k] = rayon + (1.-2*rayon)*drand48(); y[k] = rayon + (1.-2*rayon)*drand48(); } c = 0; for (j = 0; j < N_points; j++){ xx = drand48(); yy = drand48(); for (k = 0; k < N_pieces; k++){ d2 = (xx-x[k])*(xx-x[k]) + (yy-y[k])*(yy-y[k]); if (d2 <= rayon*rayon){ c++; break; } } } r = (double)c / N_points; cout << r << endl; rr += r; } cout << "La proportion moyenne occupee par " << N_pieces << " pieces avec rayon " << rayon << " est : " << rr / N_fois << endl; free(x); free(y); return 0; }