UVA 11722

You are going from Dhaka to Chittagong by train and you came to know one of your old friends is going
from city Chittagong to Sylhet. You also know that both the trains will have a stoppage at junction
Akhaura at almost same time. You wanted to see your friend there. But the system of the country is
not that good. The times of reaching to Akhaura for both trains are not fixed. In fact your train can
reach in any time within the interval [t1, t2] with equal probability. The other one will reach in any
time within the interval [s1, s2] with equal probability. Each of the trains will stop for w minutes after
reaching the junction. You can only see your friend, if in some time both of the trains is present in the
station. Find the probability that you can see your friend.


Input
The first line of input will denote the number of cases T (T < 500). Each of the following T line will
contain 5 integers t1, t2, s1, s2, w (360 ≤ t1 < t2 < 1080, 360 ≤ s1 < s2 < 1080 and 1 ≤ w ≤ 90). All
inputs t1, t2, s1, s2 and w are given in minutes and t1, t2, s1, s2 are minutes since midnight 00:00.


Output
For each test case print one line of output in the format ‘Case #k: p’ Here k is the case number and
p is the probability of seeing your friend. Up to 1e − 6 error in your output will be acceptable.


Sample Input
2
1000 1040 1000 1040 20
720 750 730 760 16


Sample Output
Case #1: 0.75000000
Case #2: 0.67111111

 

题意:你和朋友都要乘坐火车,为了在A城市见面,你会在时间区间[t1, t2]中的任意时刻以相同的概率密度到达,你的朋友会在时间区间[s1, s2]内的任意时刻以相同的概率密度到达,你们的火车都会在车站停留W秒,只有在同一时刻火车都在城市A的时候,才会相见,问你这件事情的概率

 

解题思路:建立一个直角坐标系,t1<=x<=t2 ,s1<=y<=s2这样构成的这个矩形就是所有的概率区间,然后画一条x=y的直线,然后把这个直线沿着x=y方向想下和向上平移w得到一个区间,这个区间和句型重叠的部分就是见面的概率区间,用这个面积除以矩形的面积就是见面概率,

 

 

代码如下:

 

 

 1 #include <stdio.h>
 2 #include <math.h>
 3 double s1,s2,t1,t2,w;
 4 double area(double b)
 5 {
 6     double s=(t2-t1)*(s2-s1);
 7     double y1=t1+b;
 8     double y2=t2+b;
 9     if(y2<=s1)     //表示直线交于矩形右下顶点或者以下  
10     {
11         //printf("y2<s1\n");
12         return 0;
13     }
14     if(y1<=s1)   //直线交于矩形下面边 
15     {
16         //printf("y1<s1\n");
17         if(y2<=s2)   //直线交于矩形右面边
18         {
19            // printf("y2<=s2\n");
20             return 0.5*(t2-(s1-b))*(t2+b-s1);
21         }
22         else      //直线交于矩形上面边   
23         {
24           //  printf("y2>s2\n");
25             return 0.5*(t2-(s1-b)+t2-(s2-b))*(s2-s1);
26         }
27     }
28     else if(y1<=s2)    // 直线交于矩形左面边  
29     {
30         //printf("y1<s2\n");
31         if(y2<=s2)    //直线交于矩形右面边  
32         {
33             //printf("y2<=s2\n");
34             return 0.5*((t1+b)-s1+(t2+b)-s1)*(t2-t1);
35         }
36         else     // 直线交于矩形上面边  
37         {
38             //printf("y2>s2\n");
39             return s-0.5*(s2-(t1+b))*(s2-b-t1);
40         }
41 
42     }
43     else
44     {
45       //  printf("return s\n");
46         return s;
47     }
48 }
49 int main()
50 {
51     int t;
52     scanf("%d",&t);
53     for(int cas=1; cas<=t; cas++)
54     {
55         double a,b;
56         scanf("%lf%lf%lf%lf%lf",&t1,&t2,&s1,&s2,&w);
57         a=area(w);
58        // printf("%lf\n",a);
59         b=area(-w);
60         //printf("%lf\n",b);
61         double ans=a-b;        //这里的相减是用y=x+w与矩形的下边和右边围成的面积减去y=x-w围成面积
62         ans/=(s2-s1)*(t2-t1);
63         printf("Case #%d: %.8lf\n",cas,ans);
64     }
65     return 0;
66 }

 

posted @ 2015-08-18 11:03  果冻0_0  阅读(230)  评论(0编辑  收藏  举报