题目: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;
}