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
http://blog.csdn.net/sdjzping/article/details/12954379
#include<bits/stdc++.h>
#define ANHOUR 43200
using namespace std;
struct node
{
double begin, end;
public:
void clear() {
begin = end = 0;
}
};
inline double thisMin(double x, double y, double z) {
return min(min(x, y), z);
}
inline double thisMax(double x, double y, double z) {
return max(max(x, y), z);
}
int main() {
std::ios::sync_with_stdio(false);
int n;
double p1, p2, p3, t1, t2, t3, maxT, minT, sumT;
vector<node>v1, v2, v3;
node x;
p1 = 59 * 1.0 / 10; //秒和分
p2 = 719 * 1.0 / 120; //秒和时
p3 = 11 * 1.0 / 120; //分和时
t1 = 3600.0000 / 59; //秒和分
t2 = 43200.0000 / 719; //秒和时
t3 = 43200.0000 / 11; //分和时
while (cin >> n && n >= 0)
{
v1.clear();
v2.clear();
v3.clear();
x.clear();
x.begin = n * 1.0 / p1;
x.end = t1 - x.begin;
v1.push_back(x);
while (true)
{
x.begin = x.begin + t1;
x.end = x.end + t1;
v1.push_back(x);
if (x.begin > ANHOUR && x.end > ANHOUR)
{
break;
}
}
x.clear();
x.begin = n * 1.0 / p2;
x.end = t2 - x.begin;
v2.push_back(x);
while (true)
{
x.begin += t2;
x.end += t2;
v2.push_back(x);
if (x.begin > ANHOUR && x.end > ANHOUR)
{
break;
}
}
x.clear();
x.begin = n * 1.0 / p3;
x.end = t3 - x.begin;
v3.push_back(x);
while (true)
{
x.begin += t3;
x.end += t3;
v3.push_back(x);
if (x.begin > ANHOUR && x.end > ANHOUR)
{
break;
}
}
auto f1 = v1.begin();
auto f2 = v2.begin();
auto f3 = v3.begin();
sumT = minT = maxT = 0.0;
while (minT <= ANHOUR && maxT <= ANHOUR)
{
maxT = thisMax((*f1).begin, (*f2).begin, (*f3).begin);
minT = thisMin((*f1).end, (*f2).end, (*f3).end);
if (maxT < minT) {
sumT = sumT + (minT - maxT);
}
if (minT == (*f1).end)
{
f1++;
}
else if (minT == (*f2).end)
{
f2++;
}
else
{
f3++;
}
}
cout << setprecision(3) << std::fixed << sumT / 432 << endl;
}
return 0;
}