HDOJ 1006

Tick and Tick

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5553    Accepted Submission(s): 1518


Problem Description
The three hands of the clock are rotating every second and meeting each other many times everyday. Finally, they get bored of this and each of them would like to stay away from the other two. A hand is happy if it is at least D degrees from any of the rest. You are to calculate how much time in a day that all the hands are happy.
 

Input
The input contains many test cases. Each of them has a single line with a real number D between 0 and 120, inclusively. The input is terminated with a D of -1.
 

Output
For each D, print in a single line the percentage of time in a day that all of the hands are happy, accurate up to 3 decimal places.
 

Sample Input
0
120
90
-1
 

Sample Output
100.000
0.000
6.251
 

分析公式:

时间(h:m:s)与度数(rh:rm:rs)之间的方程:

rs=6*s;          rm=6*m+s/10;           rh=30*h+0.5*m+s/120;
各针之间的角度如下:

rm-rs=6*m+(0.1-6)*s;       rh-rs=30*h+0.5*m+(1/120)-6)*s;       rh-rm=30*h+(0.5-6)*m+((1/120)-0.1)*s;

指针间的度数要在d到360-d之间,即解三个|ax+b|型的不等式:(s为唯一未知数)

可以求出任意一分钟内的秒针取值范围,然后每分钟都求一遍。

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 struct set
 4 {
 5     double a,b;
 6 };
 7 double d;
 8 struct set sloveset(double a,double b);         /* 求 d<=ax+b<360-d 的解 */
 9 struct set intersection(struct set a,struct set b);          /* 给两个集合取交集 */
10 int main()
11 {
12     int h,m,i,j,k;
13     double a1,b1,a2,b2,a3,b3,time;
14     struct set answer[3][2],ensemble;
15     while(scanf("%lf",&d)&&d!=-1)
16     {
17         time=0;
18         for(h=0; h<12; h++)
19         {
20             for(m=0; m<60; m++)
21             {
22                 b1=6.0*m;                a1=-5.9;
23                 b2=30*h+0.5*m;            a2=1.0/120-6.0;
24                 b3=30*h+(0.5-6)*m;         a3=(1.0/120)-0.1;
25 /* 求3个绝对值不等式的解集 存到answer中answer[0][0] answer[0][1]要取并集剩下两个也是*/
26                 answer[0][0]=sloveset(a1,b1);              answer[0][1]=sloveset(-a1,-b1);
27                 answer[1][0]=sloveset(a2,b2);              answer[1][1]=sloveset(-a2,-b2);
28                 answer[2][0]=sloveset(a3,b3);              answer[2][1]=sloveset(-a3,-b3);
29 /* 取过交集后,需要将3个式子的结果取并集 所以采用下面的方法
30 循环的意思就是红黄绿中各取一个求交集(上图表示数组answer)*/
31                 for(i=0;i<2;i++)
32                 {
33                     for(j=0;j<2;j++)
34                     {
35                         for(k=0;k<2;k++)
36                         {
37                             ensemble=intersection(intersection(answer[0][i],answer[1][j]),answer[2][k]);
38                             time+=ensemble.b-ensemble.a; } } }
39             }
40         }
41         time=time*100.0/(12*3600);
42         printf("%.3lf\n",time);
43     }
44     return 0;
45 }
46 struct set sloveset(double a,double b)
47 {
48     struct set seta;
49     if(a>0)
50     {
51         seta.a=(d-b)/a;
52         seta.b=(360-d-b)/a;
53     }
54     else
55     {
56         seta.b=(d-b)/a;
57         seta.a=(360-d-b)/a;
58     }
59     if(seta.a<0) seta.a=0;
60     if(seta.b>60) seta.b=60;
61     if(seta.a>=seta.b) seta.a=seta.b=0; //之前这句放到了if(seta.a<0)if(seta.b>60)前面了
62     return seta;              //结果seta.b变成了负的怀疑是seta.b太大了 冒了 不知对错
63 }
64 struct set intersection(struct set a,struct set b)
65 {
66     struct set p;
67     p.a=a.a>b.a ?a.a:b.a;
68     p.b=a.b<b.b ?a.b:b.b;
69     if(p.a>p.b) p.a=p.b=0;
70     return p;
71 }
72  
73 
74  
75  
76  

原文源自:

http://blog.sina.com.cn/s/blog_81650692010138nr.html

你可能感兴趣的:(OJ)