Tick and Tick
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
double MAX(double a, double b, double c)
{
if(a >= b && a >= c) return a;
else if(b >= c) return b;
else return c;
}
double MIN(double a, double b, double c)
{
if(a <= b && a <= c) return a;
else if(b <= c) return b;
else return c;
}
int main()
{
double s = 360. / 60., m = 360. / 3600., h = 360. / 43200.;//角速度
double m_s = s - m, h_m = m - h, h_s = s - h;//相对较速度
//cout << m_s <<" "<< h_m <<" "<< h_s << endl;
double D,x_m_s,x_h_s,x_h_m,y_m_s,y_h_s,y_h_m;//周期
double cycle_m_s = 360 / m_s;
double cycle_h_m = 360 / h_m;
double cycle_h_s = 360 / h_s;
//cout << cycle_m_s <<" "<< cycle_h_m <<" "<< cycle_h_s << endl;
while(scanf("%lf",&D) == 1 && D != -1)
{
//cout << D << endl;
x_m_s = D / m_s;//周期内合法区间
x_h_m = D / h_m;
x_h_s = D / h_s;
y_m_s = (360 - D) / m_s;
y_h_m = (360 - D) / h_m;
y_h_s = (360 - D) / h_s;
//cout << x_m_s <<" "<< y_m_s << endl;
//cout << x_h_m <<" "<< y_h_m << endl;
//cout << x_h_s <<" "<< y_h_s << endl;
double sumtime = 0;
double begintime, endtime;
//寻找交集
for(double begin1 = x_m_s,end1 = y_m_s; end1 < 43200.000001; begin1 += cycle_m_s,end1 += cycle_m_s)
{
for(double begin2 = x_h_m,end2 = y_h_m; end2 < 43200.000001; begin2 += cycle_h_m,end2 += cycle_h_m)
{
if(end1 < begin2) break;
if(end2 < begin1) continue;
for(double begin3 = x_h_s,end3 = y_h_s; end3 < 43200.000001; begin3 += cycle_h_s,end3 += cycle_h_s)
{
if(begin3 > end1 || begin3 > end2) break;
if(begin1 > end3 || begin2 > end3) continue;
begintime = MAX(begin1, begin2, begin3);
endtime = MIN(end1, end2, end3);
if(endtime > begintime) sumtime += (endtime - begintime);
}
}
}
printf("%.3f\n", sumtime / 432);
}
return 0;
}
啊····好久没写博客了····