// Q764555.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <math.h>
#include <time.h>
using namespace std;
int inshader(float x, float y)
{
if (x < 5) return 0;
if (sqrt((x - 5) * (x - 5) + (y - 5) * (y - 5)) < 5) return 0;
if (sqrt((x - 15) * (x - 15) + (y - 5) * (y - 5)) < 5) return 0;
if (x * 0.5 + y < 10) return 0;
return 1;
}
int main()
{
int N = 10000;
srand((unsigned)time(NULL));
int c = 0;
for (int i = 0; i < N; i++)
{
float x = rand() / (float)(RAND_MAX);
float y = rand() / (float)(RAND_MAX);
x *= 20;
y *= 10;
if (inshader(x, y)) c++;
}
printf("结果 %lf", 10.0 * 20 * c / (double)N);
}
结果 19.900000
Press any key to close this window . . .