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
Author
PAN, Minghao
Source
Recommend
JGShining
答案:
#include <stdio.h>
#include <cmath>
double min(double a, double b, double c)
{
if (a <= b && a <= c) return a;
else if (b <= a && b <= c) return b;
else return c;
}
double max(double a, double b, double c)
{
if (a >= b && a >= c) return a;
else if (b >= a && b >= c) return b;
else return c;
}
int main(int argc, char* argv[])
{
double D;
double R;
double t1[11][2], t2[708][2], t3[719][2],t; // 记录时间区间数组
int i, j, k;
double vs = 6, vm = 0.1, vh = 1.0/120.0; // 移动速度
double ts, te;
while (scanf("%lf", &D)!=EOF)
{
if (D==-1||D<0||D>120)
break;
t = 0;
for (i = 0; i < 11; ++i) // 分针相对时针符合的时间区间
{
t1[i][0] = (360*i+D)/(vm-vh);
t1[i][1] = (360*(i+1)-D)/(vm-vh);
}
for (j = 0; j < 708; ++j) // 秒针相对分针符合的时间区间
{
t2[j][0] = (360*j+D)/(vs-vm);
t2[j][1] = (360*(j+1)-D)/(vs-vm);
}
for (k = 0; k < 719; ++k) // 秒针相对时针符合的时间区间
{
t3[k][0] = (360*k+D)/(vs-vh);
t3[k][1] = (360*(k+1)-D)/(vs-vh);
}
i = 0, j = 0, k = 0;
while (true) // 计算重叠的时间区间
{
if (i == 11 || j == 708 || k == 719)
break;
ts = max(t1[i][0],t2[j][0],t3[k][0]);
te = min(t1[i][1],t2[j][1],t3[k][1]);
if (te>ts)
{
t += (te-ts);
if (t1[i][1]<=te&&i<11) ++i;
if (t2[j][1]<=te&&j<708) ++j;
if (t3[k][1]<=te&&k<719) ++k;
}
else
{
if (t1[i][1]<=ts&&i<11) ++i;
if (t2[j][1]<=ts&&j<708) ++j;
if (t3[k][1]<=ts&&k<719) ++k;
}
}
R = (double) t*100.0/(12*60*60.0);
printf("%.3f/n", R);
}
return 0;
}
#include <cmath>
double min(double a, double b, double c)
{
if (a <= b && a <= c) return a;
else if (b <= a && b <= c) return b;
else return c;
}
double max(double a, double b, double c)
{
if (a >= b && a >= c) return a;
else if (b >= a && b >= c) return b;
else return c;
}
int main(int argc, char* argv[])
{
double D;
double R;
double t1[11][2], t2[708][2], t3[719][2],t; // 记录时间区间数组
int i, j, k;
double vs = 6, vm = 0.1, vh = 1.0/120.0; // 移动速度
double ts, te;
while (scanf("%lf", &D)!=EOF)
{
if (D==-1||D<0||D>120)
break;
t = 0;
for (i = 0; i < 11; ++i) // 分针相对时针符合的时间区间
{
t1[i][0] = (360*i+D)/(vm-vh);
t1[i][1] = (360*(i+1)-D)/(vm-vh);
}
for (j = 0; j < 708; ++j) // 秒针相对分针符合的时间区间
{
t2[j][0] = (360*j+D)/(vs-vm);
t2[j][1] = (360*(j+1)-D)/(vs-vm);
}
for (k = 0; k < 719; ++k) // 秒针相对时针符合的时间区间
{
t3[k][0] = (360*k+D)/(vs-vh);
t3[k][1] = (360*(k+1)-D)/(vs-vh);
}
i = 0, j = 0, k = 0;
while (true) // 计算重叠的时间区间
{
if (i == 11 || j == 708 || k == 719)
break;
ts = max(t1[i][0],t2[j][0],t3[k][0]);
te = min(t1[i][1],t2[j][1],t3[k][1]);
if (te>ts)
{
t += (te-ts);
if (t1[i][1]<=te&&i<11) ++i;
if (t2[j][1]<=te&&j<708) ++j;
if (t3[k][1]<=te&&k<719) ++k;
}
else
{
if (t1[i][1]<=ts&&i<11) ++i;
if (t2[j][1]<=ts&&j<708) ++j;
if (t3[k][1]<=ts&&k<719) ++k;
}
}
R = (double) t*100.0/(12*60*60.0);
printf("%.3f/n", R);
}
return 0;
}