ural(Timus) 1333. Genie Bomber 2

几何题

题意:给出n个圆的圆心坐标和半径,给出一个单位方格,在(0,0),(0,1),问这些圆覆盖的面积占方格的百分比,圆超出方格的部分不计算。另外算百分比,答案精确到1%即可,即整数部分正确即可,小数部分不要求

这题,可以想按照题意直接下手,未免太难了,另外注意到答案的输出,其实对精度的要求很低(对小数都没要求)。我们可以用一直近似的算法来解决

将方格分割为一个一个的小格子,当分割得很小的时候,格子可以看做是一个点,然后看这个点在不在圆上或圆内,在的话相当于圆覆盖了这个点,覆盖了这个格子。所以我们将1*1方格分割为1000*1000的格子,然后逐一去判断,很暴力的方法,但就是这样过了

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

const double PI = acos(-1.0);
const double E = 1e-8;
const int N = 15;
const int M = 1000;
const double B = 0.001;
struct crc
{
    double x,y,r;
}a[N];
int n;

double dis(double x ,double y , struct crc p)
{
    return sqrt( (x-p.x)*(x-p.x) + (y-p.y)*(y-p.y));
}

int judge(double x , double y)
{
    for(int i=0; i<n; i++)
    {
        double d = dis(x,y,a[i]);
        if( d < a[i].r || fabs(d - a[i].r) < E)
            return 1;
    }
    return 0;
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        int res = 0;
        for(int i=0; i<n; i++)
            scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].r);
        for(int i=0; i<=M; i++)
            for(int j=0; j<=M; j++)
            {
                double x = B * 1. * i;
                double y = B * 1. * j;
                int ok = judge(x,y);
                res += ok;
            }
        printf("%.6lf\n",1.*res/(1.*M*M)*100.0);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/scau20110726/p/3060330.html