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
 

 

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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值