几何概率

题目:https://cn.vjudge.net/problem/UVA-11722

巧妙利用几何概型,把所有可能情况看成一个二维矩形 (t0<=x<=t1, s0<=y<=s1);

然后求面积求占比即可。。

ac代码:

#include <cstdio>
#include <math.h>
#include <algorithm>
#include <cstring>
#include <queue>
#include <iostream>
#define inf 0x3f3f3f3f
#define exp 0.00000001
using namespace std;
typedef long long ll;
const int mx = 6e3+100;

double x[3], y[3];

double sut(double w) {
    double su = 0;
    double x1 = y[0]+w, l=y[0], r=y[1];
        if (x1 < x[0]) {
            x1 = x[0];
            l = min(x[0]-w, y[1]);
            su += (x[1]-x[0])*(l-y[0]);
        }
        else if (x1 > x[1])
            x1 = x[1];
        double x2 = y[1]+w;
        if (x2 < x[0])
            x2 = x[0];
        else if (x2 > x[1]) {
            x2 = x[1];
            r = min(y[1], x[1]-w);
        }
        else
            su += (x[1]-x2)*(y[1]-l);
        su += (x2-x1)*(r-l)/2;
        return su;
}

int main() {
    int _, o = 1;
    double w;
    scanf("%d", &_);
    while (_--) {
        scanf("%lf%lf%lf%lf%lf", &x[0], &x[1], &y[0], &y[1], &w);
        double sum = (x[1]-x[0])*(y[1]-y[0]), su = 0;

        printf("Case #%d: %.8f\n", o++, (sut(-w)-sut(w))/sum);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值