题意是说求出在一天中时针、分针、秒针之间距离均在 D 度以上的时间占比。
由于三针始终都在转动,所以要分别求出各个针之间的相对角速度,分别求出三针满足角度差的首次时间,再分别求出不满足角度差的最终时间,取这三个时间段的交集,也就是首次时间的最大值和最终时间的最小值之间的部分,要注意剪枝,去掉多余的部分,否则会超时的,
本人的代码也是借鉴了别人写出来的。代码如下:
1 #include <bits/stdc++.h> 2 using namespace std; 3 const double hm = 11.0/120;//时针分针相对角速度hm = m - h = 360/3600 - 360/(12*3600) = 11/120 4 const double hs = 719.0/120;//时针秒针相对角速度hs = s - h = 360/60 - 360/(12*3600) = 719/120 5 const double ms = 59.0/10;//分针秒针相对角速度ms = s - m = 360/60 - 360/3600 = 59/10 6 const double thm = 43200.0/11;//thm = 360/hm = 43200/11 7 const double ths = 43200.0/719;//ths = 360/hs = 43200/719 8 const double tms = 3600.0/59;//tms = 360/ms = 3600/59 9 const double eps = 1e-6; 10 double max(double a,double b,double c) 11 { 12 if(a>b) return a>c?a:c; 13 return b>c?b:c; 14 } 15 double min(double a,double b,double c) 16 { 17 if(a<b) return a<c?a:c; 18 return b<c?b:c; 19 } 20 int main() 21 { 22 int deg; 23 double from1,from2,from3,to1,to2,to3,x1,x2,x3,y1,y2,y3,st,ed,ans; 24 while(scanf("%d",°)) 25 { 26 if(deg==-1) break; 27 from1 = deg/hm; 28 from2 = deg/hs; 29 from3 = deg/ms; 30 to1 = (360-deg)/hm; 31 to2 = (360-deg)/hs; 32 to3 = (360-deg)/ms; 33 ans = 0.0; 34 for(x1 = from1, y1 = to1; y1 <= 43200+eps; x1+=thm, y1+=thm) 35 for(x2 = from2, y2 = to2; y2 <= 43200+eps; x2+=ths, y2+=ths) 36 { 37 if(x2>y1) break; 38 for(x3 = from3, y3 = to3; y3 <= 43200+eps; x3+=tms, y3+=tms) 39 { 40 if(x3>y1 || x3>y2) break; 41 st = max(x1,x2,x3); 42 ed = min(y1,y2,y3); 43 if(st<ed) ans+=ed-st; 44 } 45 } 46 printf("%.3lf\n",ans*100.0/43200);//求出时间和的占比 47 } 48 return 0; 49 }
原文:https://www.cnblogs.com/Taskr212/p/9510825.html