2022 01.18 第二天
Tick and Tick
滴答滴答
1000ms 32768K
描述:
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.
这个时钟的3个指针每秒都在流动并且在每一天相遇许多次。最后,他们变得对于这些感到无聊并且他们中的每一个都都想远离其他两个。如果它至少是与其他相差D度那么这个指针是开心的。你需要计算在一天有多少时间是所有的指针是开心的。
输入:
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.
输入包含多组测试样例。他们中的每一个都有一行输出一个D值在0到120之间,包括的。这个输入以D为-1时结束。
输出:
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.
对于每个D,在一行输出在一天所有的指针都开心的时间百分比,估计到小数点后三位。
样例输入:
0 120 90 -1
样例输出:
100.000 0.000 6.251
#include<iostream>
#include<cstdio>
using namespace std;
inline double max(double a,double b,double c)
{
a= a > b ? a: b;
a= a > c ? a: c;
return a;
}
inline double min(double a,double b,double c)
{
a= a < b ? a: b;
a= a < c ? a: c;
return a;
}
int main()
{
int i,j;
double dsm[1444],dsh[1444],dmh[26];//用于记录表的指针距离d的起始与终结时间
double d,sum,x,y;
double sm,mh,sh;
int smf[2],mhf[2],shf[2];
sm=3600.0000/59;//秒针与分针重合一次所需时间
sh=43200.0000/719;//秒针与时针重合一次所需时间
mh=43200.0000/11;//时针与分针重合一次所需时间
while(cin>>d && d!=-1)
{
sum=0;x=0;y=0;
dsm[0]=(10*d)/59;//记录第一次秒针与分针距离d的时间
dsh[0]=(120*d)/719;//记录第一次秒针与时针距离d的时间
dmh[0]=(120*d)/11;//记录第一次分针与时针距离d的时间
dsm[1]=sm-dsm[0];//记录第一次秒针与时针距离开始小于d的时间
dsh[1]=sh-dsh[0];//记录第一次秒针与时针距离开始小于d的时间
dmh[1]=mh-dmh[0];//记录第一次秒针与时针距离开始小于d的时间
for(i=2,j=3;;i+=2,j+=2)
{
dsm[i]=dsm[i-2]+sm;//下一次距离大于等于d的时间
dsm[j]=dsm[j-2]+sm;//下一次距离小于d的时间
if(dsm[i]>43200 && dsm[j]>43200) break;
}//分别记录下每一次距离d和开始小于d的时间
for(i=2,j=3;;i+=2,j+=2)
{
dsh[i]=dsh[i-2]+sh;//下一次距离大于等于d的时间
dsh[j]=dsh[j-2]+sh;//下一次距离小于d的时间
if(dsh[i]>43200 && dsh[j]>43200) break;
}//分别记录下每一次距离d和开始小于d的时间
for(i=2,j=3;;i+=2,j+=2)
{
dmh[i]=dmh[i-2]+mh;//下一次距离大于等于d的时间
dmh[j]=dmh[j-2]+mh;//下一次距离小于d的时间
if(dmh[i]>43200 && dmh[j]>43200) break;
}//分别记录下每一次距离d和开始小于d的时间
smf[0]=mhf[0]=shf[0]=0;
smf[1]=mhf[1]=shf[1]=1;//初始化,以第一次为基点
while(y<=43200 && x<=43200)
{
x=max(dsm[smf[0]],dsh[shf[0]],dmh[mhf[0]]);//起点找大者
y=min(dsm[smf[1]],dsh[shf[1]],dmh[mhf[1]]);//终点找小者
if(x<y) sum+=y-x;//一段一段的累加满足的时间
if(y==dsm[smf[1]]) { smf[0]+=2; smf[1]+=2; }//转向下一次
if(y==dsh[shf[1]]) { shf[0]+=2; shf[1]+=2; }//转向下一次
if(y==dmh[mhf[1]]) { mhf[0]+=2; mhf[1]+=2; }//转向下一次
}
printf("%.3f\n",sum/432); //实际上只计算了12个小时,不过比例可以替代一天
}
return 0;
}