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
分析公式:html
時間(h:m:s)與度數(rh:rm:rs)之間的方程:數組
rs=6*s; rm=6*m+s/10; rh=30*h+0.5*m+s/120;
各針之間的角度以下:app
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;this
指針間的度數要在d到360-d之間,即解三個|ax+b|型的不等式:(s爲惟一未知數)spa
能夠求出任意一分鐘內的秒針取值範圍,而後每分鐘都求一遍。指針
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
原文源自:rest