hdu1006之模拟

来源:互联网 发布:js array删除指定元素 编辑:程序博客网 时间:2024/05/22 06:36

重磅炸弹——网易游戏雷火盘古校园招聘开始!

Tick and Tick

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


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
012090-1
 

Sample Output
100.0000.0006.251
 

Author
PAN, Minghao
 

Source
ZJCPC2004
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  1007 1008 1010 1009 1011 

题目大意:时钟有三个针,秒针,分针,时针,现在给定一个度数,问这三个针两两之间的度数都超过n度的
时间占一天时间的百分比,结果保留三位小数。

题目分析:最开始看到这个题,第一反应是枚举每一秒,然后判断是否满足条件,但是发现结果与样例差很
多。仔细揣摩了一下题目的意思,发现每个针都是在连续地走(不停地走),然后想到这里就悲
剧了,看了大神的代码,看了开头就不想看下去了,终于今天强迫自己把这道题啃掉了。

代码:
#include <iostream>#include <stdio.h>using namespace std;int main(){    //freopen("in.txt","r",stdin);    //freopen("out->txt","w",stdout);    ///时针,分针,秒针的角速度    double w_s=6.0,w_m=1.0/10,w_h=1.0/120;    ///相对角速度    double w_hm=w_m-w_h;    double w_hs=w_s-w_h;    double w_ms=w_s-w_m;    ///两针从重合到再次重合所需要的时间    double T_hm=360.0/w_hm;    double T_hs=360.0/w_hs;    double T_ms=360.0/w_ms;    ///一天的总时间    double T_all=24.0*60.0*60.0;    ///度数    double n;    while(scanf("%lf",&n)!=EOF)    {        if(n==-1)   break;        ///两针从重合到分离n度所需要的时间        double t_hm1=n/w_hm;        double t_hs1=n/w_hs;        double t_ms1=n/w_ms;        ///两针从重合到再次相差n度所需要的时间        double t_hm2=(360.0-n)/w_hm;        double t_hs2=(360.0-n)/w_hs;        double t_ms2=(360.0-n)/w_ms;        double ans=0;        for(double hs=0.0;hs<=T_all;hs+=T_hs){            for(double hm=0.0;hm<=T_all;hm+=T_hm){                if(hm+t_hm2<hs+t_hs1)   continue;                if(hs+t_hs2<hm+t_hm1)   break;                for(double ms=0.0;ms<=T_all;ms+=T_ms){                    if(ms+t_ms2<hs+t_hs1||ms+t_ms2<hm+t_hm1)    continue;                    if(hs+t_hs2<ms+t_ms1||hm+t_hm2<ms+t_ms1)    break;                    double res1=max(max(hs+t_hs1,hm+t_hm1),ms+t_ms1);                    double res2=min(min(hs+t_hs2,hm+t_hm2),ms+t_ms2);                    if(res1<res2)   ans+=res2-res1;                }            }        }        printf("%.3lf\n",ans/T_all*100.0);    }    return 0;}