题目介绍
-
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
题目分析
根据三个指针的角速度不同来计算相应的时间。不能使用每秒的方式来计算,这样得到结果差别较大。代码中的 periodSH 在一圈也就是 360 度内,到达同样相隔角度所需的时间,其实也就是快的指针比慢的指针多走了 360 度,这样就容易理解多了。
代码
#include <iostream>
#include <iomanip>
using namespace std;
const double vS = 6;
const double vM = 1/10.0;
const double vH = 1/120.0;
const double deltaVSH = vS - vH;
const double deltaVSM = vS - vM;
const double deltaVMH = vM - vH;
const double periodSH = 360 / deltaVSH;
const double periodSM = 360 / deltaVSM;
const double periodMH = 360 / deltaVMH;
const double halfDay = 12 * 60 * 60;
#define MAX(a,b,c) (a>b?(a>c?a:c):(b>c?b:c))
#define MIN(a,b,c) (a<b?(a<c?a:c):(b<c?b:c))
int main()
{
int degree = 0;
double bSH=0, bSM=0, bMH=0;
double eSH=0, eSM=0, eMH=0;
double time = 0;
while(cin>>degree && degree!=-1)
{
time = 0;
bSH = degree / deltaVSH;
bSM = degree / deltaVSM;
bMH = degree / deltaVMH;
eSH = (360 - degree) / deltaVSH;
eSM = (360 - degree) / deltaVSM;
eMH = (360 - degree) / deltaVMH;
for(double btSH=bSH, etSH=eSH; etSH < halfDay+0.0000001; btSH+=periodSH, etSH+=periodSH)
{
for(double btSM=bSM, etSM=eSM; etSM < halfDay+0.0000001; btSM+=periodSM, etSM+=periodSM)
{
if(etSM < btSH)
{
continue;
}
if(btSM > etSH)
{
break;
}
for(double btMH=bMH, etMH=eMH; etMH < halfDay+0.0000001; btMH+=periodMH, etMH+=periodMH)
{
if(etMH<btSM || etMH<btSH)
{
continue;
}
if(btMH>etSH || btMH>etSM)
{
break;
}
time += MIN(etSH,etSM,etMH) - MAX(btSH,btSM,btMH);
}
}
}
cout.setf(ios::fixed);
cout<<setprecision(3)<<time/halfDay*100<<endl;
}
return 0;
}