HDU-Tick and Tick

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


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

一开始没看懂题意,后来看了大神的题解后,恍然大悟,

既然是3的针两两成角度,就列出两两Happy的时间,取交集

#include<iostream>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
const double vh = 1. /  double(120.);
const double vm = 1. / double(10.);
const double vs = 6.;
#define ms(a,b) memset(a,b,sizeof(a))
int main()
{
    double n;
    double T[3] = {(360. / (vm - vh)), (360. / (vs - vm)), (360. / (vs - vh))};
    //printf("T[0] = %lf , T[1] = %lf T[2] = %lf\n", T[0], T[1], T[2] );
    while (scanf("%lf",&n), n != -1) {
        double Hs[3] = {(n / 360.) * T[0], (n / 360.) * T[1], (n / 360.) * T[2]};//每对针开始Happy的时间
        double He[3] = {(360. - n) / 360. * T[0], (360. - n) / 360. * T[1], (360. - n) / 360. * T[2]};//每对针结束的时间
        double HappyTime = 0., NextHs = Hs[0], NextHe = min (He[1], He[2]);
        while(Hs[1] < 43200 - (n / 360.) * T[0] && Hs[2] < 43200 - (n / 360.) * T[0]) {//结束条件时两对针的开始时间加上最开始的另一根针的开始时间小于43200(12小时)
            NextHs = max (Hs[0], max (Hs[1], Hs[2]));
            NextHe = min (He[0], min (He[1], He[2]));
            if (NextHe > NextHs)
                HappyTime += (NextHe - NextHs);
            //printf("Hs[0] = %lf Hs[1] = %lf Hs[2] = %lf\n",Hs[0] , Hs[1], Hs[2]);
            //printf("He[0] = %lf He[1] = %lf He[2] = %lf\n", He[0], He[1], He[2]);
            //printf("NextHs = %lf NextHe = %lf HappyTime = %lf\n",NextHs, NextHe, HappyTime );
            for (int i = 0; i <= 2; i++) {
                if (NextHe == He[i]) {
                    He[i] += T[i];
                    Hs[i] += T[i];
                }
            }
        }
        double result = HappyTime / 432.;
        printf("%.3lf\n", result);
    }
    return 0;
}

注意一下,Hs【i】加的时候,是根据nextHe的值是否==He[i]来的,应为如果他俩单独加的话,Hs[i]的值会持续增大,而根据

NExtHe的话Hs[i]不会是某个值的持续增大,

比如分针和时针的Happy时间是 1 - 100 (比如)

分针和秒针的Happy时间是 2 - 50  

秒针的时针的Happy是 3 - 25 

而 他们的周期分别是 T[0]=100,T[1]=50,T[2]=20;

那么第一次肯定交集是3 -- 25 ,HappyTime += ( 25 -3 ) ;

那么秒针和时针的Happy时间肯定要往后推一个周期 变成 23 -- 45

那么交集仍是秒针和时针的 13 -- 35 HappyTime += (35 -13 ) ;

而后 秒针和时针变为 43 -- 65

那么交集变为 43 -- 50 

Happy Time+= ( 50 -43 );

那么以后就是分针和秒针往后推一个周期,以此类推,

所以要用NextHe来推。。。

猜你喜欢

转载自blog.csdn.net/henu_jizhideqingwa/article/details/80552305
今日推荐