Tick and Tick

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


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
 

思想:

由于要记录连续时间,那么就利用区间记录,[begin,end]。另外,就拿时针和分针来做例子吧。时针的角速度为w_h=360.0/12*60*60=1.0/120,分针的角速度为w_m=360.0/60*60=1.0/10。两者的相对角速度为w_hm=w_m-w_h,“相对周期”为T_hm=360.0/w_hm。所谓的相对周期就是时针和分针出现重复的相对关系的最小时间。

这样的话就可以把时针和分针,时针和秒针,分针和秒针各自满足条件的集合求交集。显然,代码中利用的是三个for循环来暴力解决。不过有些显然不满足条件的情况即可去除,以减少计算次数。

x[3]和y[3]记录的是第一个开始满足条件的时间和第一个开始不满足条件的时间。m[3],n[3]则是分表根据相对周期来扩大x[3],y[3]以获得所有满足条件的时间集合,并求交集。


代码:
#include<iostream>
#include<iomanip>
#include <algorithm> using namespace std; const double hm = 11.0/120,
hs = 719.0/120,
sm = 59.0/10; //相对角速度
const double T_hm = 43200.0/11,
T_hs = 43200.0/719,
T_sm = 3600.0/59; //相对周期 inline double min(double a,double b,double c)
{
double temp[3] = {a,b,c};
return *std::min_element(temp,temp+3);
} inline double max(double a,double b,double c)
{
double temp[3] = {a,b,c};
return *std::max_element(temp,temp+3);
} int main()
{
double degree;
double x[3],y[3];
double m[3],n[3];
double end,begin,sum; while(cin>>degree , degree!=-1)
{
x[0]=degree/hm;
x[1]=degree/hs;
x[2]=degree/sm; y[0]=(360-degree)/hm;
y[1]=(360-degree)/hs;
y[2]=(360-degree)/sm; sum=0.0;
for(m[0]=x[0],n[0]=y[0];n[0]<=43200.000001;m[0]+=T_hm,n[0]+=T_hm)
{
for(m[1]=x[1],n[1]=y[1];n[1]<=43200.000001;m[1]+=T_hs,n[1]+=T_hs)
{
for(m[2]=x[2],n[2]=y[2];n[2]<=43200.000001;m[2]+=T_sm,n[2]+=T_sm)
{
begin=max(m[0],m[1],m[2]);
end=min(n[0],n[1],n[2]); if(end>begin)
sum+=end-begin;
}
}
} cout<<setiosflags(ios::fixed)<<setprecision(3)<<sum*100.0/43200<<endl;
} return 0;
}

用库尼玛 超时自己实现比较

#include<iostream>
#include<iomanip>
using namespace std; const double hm=11.0/120,hs=719.0/120,sm=59.0/10; //相对角速度
const double T_hm=43200.0/11,T_hs=43200.0/719,T_sm=3600.0/59; //相对周期 inline double min(double a,double b,double c)
{
double temp=(a>b)?b:a; return (c>temp)?temp:c;
} inline double max(double a,double b,double c)
{
double temp=(a>b)?a:b; return (c>temp)?c:temp;
} int main()
{
double degree;
double x[3],y[3];
double m[3],n[3];
double end,begin,sum; while(cin>>degree , degree!=-1)
{
x[0]=degree/hm;
x[1]=degree/hs;
x[2]=degree/sm; y[0]=(360-degree)/hm;
y[1]=(360-degree)/hs;
y[2]=(360-degree)/sm; sum=0.0;
for(m[0]=x[0],n[0]=y[0];n[0]<=43200.000001;m[0]+=T_hm,n[0]+=T_hm)
{
for(m[1]=x[1],n[1]=y[1];n[1]<=43200.000001;m[1]+=T_hs,n[1]+=T_hs)
{
if(n[0]<m[1])
break;
if(m[0]>n[1])
continue; for(m[2]=x[2],n[2]=y[2];n[2]<=43200.000001;m[2]+=T_sm,n[2]+=T_sm)
{
if(n[0]<m[2] || n[1]<m[2])
break;
if(m[0]>n[2] || m[1]>n[2])
continue; begin=max(m[0],m[1],m[2]);
end=min(n[0],n[1],n[2]); if(end>begin)
sum+=end-begin;
}
}
} cout<<setiosflags(ios::fixed)<<setprecision(3)<<sum*100.0/43200<<endl;
} return 0;
}

HDU 1006 Tick and Tick 时钟指针问题的更多相关文章

  1. HDU 1006 Tick and Tick(时钟,分钟,秒钟角度问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1006 Tick and Tick Time Limit: 2000/1000 MS (Java/Oth ...

  2. hdu 1006 Tick and Tick 有技巧的暴力

    Tick and Tick Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  3. hdu 1006 Tick and Tick

    Tick and Tick Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. hdu1006 Tick and Tick

    原题链接 Tick and Tick 题意 计算时针.分针.秒针24小时之内三个指针之间相差大于等于n度一天内所占百分比. 思路 每隔12小时时针.分针.秒针全部指向0,那么只需要计算12小时内的百分 ...

  5. HDU 1006 模拟

    Tick and Tick Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  6. HDU 1006 [Tick Tick]时钟问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1006 题目大意:钟表有时.分.秒3根指针.当任意两根指针间夹角大于等于n°时,就说他们是happy的, ...

  7. [ACM_模拟] HDU 1006 Tick and Tick [时钟间隔角度问题]

    Problem Description The three hands of the clock are rotating every second and meeting each other ma ...

  8. HDU 1006 Tick and Tick 解不等式解法

    一開始思考的时候认为好难的题目,由于感觉非常多情况.不知道从何入手. 想通了就不难了. 能够转化为一个利用速度建立不等式.然后解不等式的问题. 建立速度,路程,时间的模型例如以下: /******** ...

  9. HDU 1006(时钟指针转角 **)

    题意是说求出在一天中时针.分针.秒针之间距离均在 D 度以上的时间占比. 由于三针始终都在转动,所以要分别求出各个针之间的相对角速度,分别求出三针满足角度差的首次时间,再分别求出不满足角度差的最终时间 ...

随机推荐

  1. Java:编码与乱码问题

    一.为什么要编码? 由于人类的语言太多,因而表示这些语言的符号太多,无法用计算机的一个基本的存储单元----byte来表示,因而必须要经过拆分或一些翻译工作,才能让计算机能理解. byte一个字节即8 ...

  2. 计蒜客 2019 蓝桥杯省赛 B 组模拟赛(一)

    D题:马的管辖 二进制枚举方案.判断该方案是否全部能被覆盖,将最优方案存下来并进行剪枝. #include<iostream> #include<cstring> #inclu ...

  3. P3806 【模板】点分治1

    一道淀粉质的模版题,开始是暴力 #include <bits/stdc++.h> #define up(i,l,r) for(register int i = (l); i <= ( ...

  4. SAS 选取部分观测

    SAS  对部分观测得处理 在建立新数据集时,有以下两种方式可以从已经存在的数据集中选取观测到新数据集中. ·通过删除不满足条件的观测来保留想要的观测. ·仅接受满足条件的观测. 条件可以由IF语句. ...

  5. JS入门经典第四章总结

    charAt():该函数有一个参数,即选择哪一个位置上的参数.返回值就是该位置上的字符. charCodeAt():该函数有一个参数,即选择哪一个位置上的参数.返回值是该位置字符在Unicode字符集 ...

  6. 基于UML网络教学管理平台模型的搭建

    一.基本信息 标题:基于UML网络教学管理平台模型的搭建 时间:2013 出版源:网络安全技术与应用 领域分类:UML:网络教学管理平台:模型 二.研究背景 问题定义:网络教学管理平台模型的搭建 难点 ...

  7. 基于jmeter的性能测试平台(一)分布式jmeter搭建

    (1)概述 一台windows虚拟机作为controller,3台Linux虚拟机作为agent. 第一步是在所有虚拟机上安装JDK,版本最好是一样的,然后就是下载安装jmeter,网上资料很多这里不 ...

  8. Android程序backtrace分析方法

    如何分析Android程序的backtrace 最近碰到Android apk crash的问题,单从log很难定位.从tombstone里面得到下面的backtrace. *** *** *** * ...

  9. Spring5中的DispatcherServlet初始化

    Spring MVC像许多其它Web框架,被设计围绕前端控制器(DispatcherServlet)实际的工作是由可配置的,委托组件执行提供了一种用于请求处理的共享算法.这个模型是灵活的,支持不同的工 ...

  10. RichText 富文本开源项目总结

    在Android开发中,我们不免会遇到富文本的编辑和展示的需求,以下是本人之前star的富文本编辑器的开源项目,供大家参考: 一.RichEditor 开源项目地址:https://github.co ...