Tick and Tick

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20120    Accepted Submission(s): 5262

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
分析:

三个指针走的角速度:

秒针速度S = 6°/s,分针速度M = (1/10)°/s,时针速度H = (1/120)°/s

这三个指针两两之间的相对速度差为:

秒时相差S_H = (719/120)°/s,秒分相差S_M = (59/10)°/s,分时相差M_H = (120/11)°/s

相差一度需要的时间为

秒时相差SH = (120/719)s/度,秒分相差SM = (10/59)s/度,分时相差MH = (120/11)s/度

相差360°需要的时间为

秒时相差tSH = 43200.0/719,秒分相差tSM = 3600.0/59,分时相差tMH = 43200.0/11

算出两两指针在43200s(12小时)内满足条件时间的区间的交集。

代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>

using namespace std;
///秒针角速度 6度/s, 分针0.1度/s,时针1/120度/s
const double sh = 719.0/120 , sm = 59.0/10, mh = 11.0/120;///三个指针两两相对角速度。
const double tsh = 43200.0 / 719, tsm = 3600.0 / 59, tmh = 43200.0 / 11;///三个指针两两相差360度所需的时间。
///将相对角速度变成周期。(即两针间需要多久出现夹角的循环)
/// 同样可求得三个周期的最小公倍数为 43200 秒,即12小时,

double max1(double a, double b, double c)
{
return max(a, max(b, c));
}
double min1(double a, double b, double c)
{
return min(a, min(b, c));
}
int main(void)
{
int D;

while(scanf("%d", &D), D != -1)
{
double bsh, bsm, bmh, esh, esm, emh, total, beginn, endd;
///第一次满足条件的时间。(开始时间)
bsh = D / sh;
bsm = D / sm;
bmh = D / mh;
///第一次出现不满足条件的时间。(结束时间)
esh = (360 - D) / sh;
esm = (360 - D) / sm;
emh = (360 - D) / mh;

total = 0;

for(double bt1 = bsh, et1 = esh; et1<= 43200.000001; bt1 += tsh, et1 += tsh)
{
for(double bt2 = bsm, et2 = esm; et2 <= 43200.000001; bt2 += tsm, et2 += tsm)
{
///判断是否有交集。
if(bt2 > et1)
break;

if(et2 < bt1)
continue;

for(double bt3 = bmh, et3 = emh; et3 <= 43200.000001; bt3 += tmh, et3 += tmh)
{
if(bt3 > et1 || bt3 > et2)
break;

if(et3 < bt1 || et3 < bt2)
continue;

beginn = max1(bt1, bt2, bt3);
endd = min1(et1, et2, et3);

total += (endd - beginn);
}

}
}

printf("%.3f\n", total / 432);

}
return 0;
}

最新文章

  1. 简述block
  2. Facebook的体系结构分析---外文转载
  3. JS_ECMA基本语法中的几种封装的小函数-1
  4. HDU 4421 Bit Magic (图论-2SAT)
  5. 高效开发 Android App 的 10 个建议(转)
  6. Dev GridControl导出
  7. PHP 中的 9 个魔术方法
  8. Python参数中的*和**
  9. php实现base64编码
  10. Unity CCTween UGUI 动画插件
  11. 使用libcurl进行文件上传
  12. file.go
  13. 向量的L2范数求导
  14. Git -- 使用GitHub
  15. 【Hadoop学习之六】MapReduce原理
  16. 数据结构:IO读写频繁的青睐,B树和B+树
  17. 【图像处理】基于OpenCV底层实现的直方图匹配
  18. MKAnnotationView和MKPinAnnotationView的区别
  19. CSS 小结笔记之三种样式表
  20. express 调优的一个过程和心得,不错的文章

热门文章

  1. 【C&amp;数据结构】---关于链表结构的前序插入和后序插入
  2. 初识runtime
  3. python爬虫——selenium+chrome使用代理
  4. python条件与循环-条件
  5. 添加动态输出 Adding Dynamic Output 精通ASP-NET-MVC-5-弗瑞曼 Listing 2-7
  6. 通过识别页面内容获得返回值判断后续执行(exists指令的用法)
  7. 请求参content-type的值为json,返回报错的解决方法
  8. influxdb+Grafana+jmeter监控搭建
  9. CountDownLatch CyclicBarrier和 Semaphore
  10. DNS隧道工具:iodine使用