A在[S1,S2]到达,B在[T1,T2]到达,到达后都会等W分钟,问相遇的概率。
概率论课本上的题目了,列出三个方程s1<=S<=s2,t1<=T<=t2,|T-S|<=w。然后可以转化为几何图形求面积,需要讨论求解。
1 #include <string.h> 2 #include <stdio.h> 3 int cas, t1, t2, s1, s2, w; 4 double area(int w){ 5 int lc = t1+w, rc = t2+w, uc = s2-w, dc = s1-w; 6 if (lc >= s2) return 0; 7 if (rc <= s1) return (t2-t1)*(s2-s1); 8 bool bl = (lc>=s1 && lc<=s2); 9 bool br = (rc>=s1 && rc<=s2); 10 bool bu = (uc>=t1 && uc<=t2); 11 bool bd = (dc>=t1 && dc<=t2); 12 if (bl&&bu) return (lc-s2)*(lc-s2)*0.5; 13 if (bl&&br) return (t2-t1)*(s2-lc+s2-rc)*0.5; 14 if (bd&&bu) return (s2-s1)*(uc-t1+dc-t1)*0.5; 15 if (bd&&br) return (t2-t1)*(s2-s1)-(rc-s1)*(rc-s1)*0.5; 16 return 0; 17 } 18 int main(){ 19 scanf("%d", &cas); 20 for (int ca = 1; ca <= cas; ca++) { 21 scanf("%d%d%d%d%d", &t1, &t2, &s1, &s2, &w); 22 printf("Case #%d: %.7f\n", ca, (area(-w)-area(w))/(t2-t1)/(s2-s1)); 23 } 24 return 0; 25 }