HDU 1006时钟三针相差d度问题(枚举、区间)

来源:互联网 发布:centos 删除目录 编辑:程序博客网 时间:2024/06/05 05:41

Tick and Tick

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

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


思路

这题作为小白的我,一开始一脸蒙蔽,直接想成了先走秒针再走分针再走时针。然而结果是可想而知。

1、正确思路应该是,三针一起动,秒针每秒6度,分钟每秒0.1度,时针每秒1/120度double s=6.0;double m=0.1;double h=1./120;

2、因为一天24小时,而一周12小时,因为题目最后要求是求的是百分比,所以直接拿12小时来求没问题。如果求总时间那就要乘2

3、通过枚举每个重合到重合的时间段,求出每段中符合条件的一段即可

4、算出两两指针的角速度之差,然后算出两两之间的相对周期(即两指针多久出现夹角循环的周期)

5、输入d度后,需要计算第一次满足条件的时间(两针分离到n度的时间),和第一次不满足条件的时间(就是两针分离了360-d度所需要的时间(之后就不符合了))

6、通过相对角速度m_h,s_h,s_m,我们可以求出从重合到分离n度所需的时间n/m_h,n/s_h,n/s_m,以及到再重合前n度所需的时间(360-n)/m_h,(360-n)/s_h,(360-n)/s_m

7、利用三重循环要清楚一点是开始的bs_m肯定是小于结束的em_h和es_h(其他同理)用break,但是bm_h和bs_h是有可能大于es_m(其他同理)用continue

8、利用区间交集的方法将三个时间段完成分离的最大时间和三个时间段刚好有一个不成立条件的最小时间去交集,之间就是所要求的

9、最后只需要每次循环区间之间减一下即可


C解法

#include <stdio.h>double max(double a,double b,double c){    double max=a;    if(b>max)    {        max=b;    }    if(c>max)    {        max=c;    }    return max;}double min(double a,double b,double c){    double min=a;    if(b<min)    {        min=b;    }    if(c<min)    {        min=c;    }    return min;}int main(){    double s=6.0;    double m=0.1;    double h=1./120;    double s_m=59./10;//角速度之差    double s_h=719./120;    double m_h=11./120;    double Ts_m=360./s_m;//周期(即两针需要多久出现夹角循环的周期)    double Ts_h=360./s_h;    double Tm_h=360./m_h;    double d;    while(scanf("%lf",&d))    {        double bs_m,bs_h,bm_h,es_m,es_h,em_h;        double i,j,k;        double beginmax,endmin;        double total=0;        //计算第一次满足条件的时间        bs_m=d/s_m;//两针分离到n度的时间        bs_h=d/s_h;        bm_h=d/m_h;        //计算第一次不满足条件的时间,就是两针分离了360-d度所需要的时间(之后就不符合了)        es_m=(360-d)/s_m;//Ts_m-bs_m        es_h=(360-d)/s_h;        em_h=(360-d)/m_h;        if(d==-1)        {            break;        }        for(i=0;i<43200*1.0;i+=Tm_h)        {            for(j=0;j<43200*1.0;j+=Ts_h)            {                //m_h>s_h>s_m这个要理解                if(j+bs_h>i+em_h)//p>=j+bs_h&&q<=i+em_h=>p>q=>无效 (下面都是这个意思)                    break;                if(i+bm_h>j+es_h)//bm_h>es_h还是有可能的,所以用continue                    continue;                for(k=0;k<43200*1.0;k+=Ts_m)                {                    if(k+bs_m>i+em_h||k+bs_m>j+es_h)                        break;                    if(i+bm_h>k+es_m||j+bs_h>k+es_m)                        continue;                    //得到区间[max,min]                    beginmax=max(i+bm_h,j+bs_h,k+bs_m);////在这三个时间段刚好完成分离n度,所以取最大值才能保证全都分离n以上                    endmin=min(i+em_h,j+es_h,k+es_m);//在这三个时间段刚好完成合并n度,所以取最小值才能保证全都未合并到n以内                    //if(endmin>beginmax)                    total+=(endmin-beginmax);                }            }        }        printf("%.3lf\n",total*100.0/43200.0);    }    return 0;}

总结

1、时钟问题要考虑到三针同时走的问题

2、有些时候利用枚举简单暴力也是不错的

原创粉丝点击