Problem Description
The three hands of the clock arerotating every second and meeting each other many times everyday. Finally, theyget 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 tocalculate 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 thepercentage of time in a day that all of the hands are happy, accurate up to 3decimal places.
本题将三个指针的角度均小于D的时间累加起来即可;
用SH,SM, MH来表示秒针时针,秒针分针,分针时针的相对速度;
将秒针时针,秒针分针,分针时针相差度数小于D的时间用
b1,e1; b2, e2; b3, e2表示取交集并累加
以下代码:
#include<iostream>
#include<algorithm>
#include<stdio.h>
usingnamespace std;
constdouble SH = 719.0/120, SM = 59.0/10, MH = 11.0/120;
constdouble tSH = 43200.0/719, tSM = 3600.0/59, tMH = 43200.0/11;
doubleMin(double a,double b,double c)
{
return min(a, min(b, c));
}
doubleMax(double a,double b,double c)
{
return max(a, max(b, c));
}
intmain()
{
double D;
while(scanf("%lf", &D)&& D != -1){
double sumtime = 0, begin, end;
double bSM, bSH, bMH, eSM, eSH,eMH;
bSH = D / SH;
bSM = D / SM;
bMH = D / MH; // 第一次的开始时间
eSH = (360 - D) / SH;
eSM = (360 - D) / SM;
eMH = (360 - D) / MH; // 第一次的结束时间
// 需满足条件
for (double b1 = bMH, e1 = eMH; e1<= 43200.000001; b1 += tMH, e1 += tMH)
{
for (double b2 = bSM, e2 =eSM; e2 <= 43200.000001; b2 += tSM, e2 += tSM)
{
if (b1 > e2)
continue;
if (b2 > e1)
break;
for (double b3 =bSH, e3 = eSH; e3 <= 432000.000001; b3 += tSH, e3 += tSH)
{
if (b2 >e3 || b1 > e3)
continue;
if (b3 >e2 || b3 > e1)
break;
begin =Max(b1, b2, b3);
end = Min(e1,e2, e3);
sumtime +=end - begin;
}
}
} //将交集的时间累加;
printf("%.3lf\n",sumtime / 432);
}
}