HDOJ 1006

Tick and Tick

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
ZJCPC2004

解题思路
刚开始我的思路是暴力,循环个43200次求个几率,后来发现精度不够,精度够了时间又不够。查询后发现是通过循环求交集。
AC代码来自https://www.cnblogs.com/Lyush/

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;

const double s_h = 719. / 120, s_m = 59. / 10, m_h = 11. / 120;
// 三根指针间的相对角速度 

const double tsh = 43200. / 719, tsm = 3600. / 59, tmh = 43200. / 11;
// 将相对角速度变成周期。(即两针间需要多久出现夹角的循环)
// 同样可求得三个周期的最小公倍数为 43200 秒,即12小时,呵呵,意料之中啊。

inline double MAX( double a, double b, double c )
{
    double max = a;
    if( b > max )
        max = b;
    if( c > max )
        max = c;
    return max;
}

inline 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 D;
    while( scanf( "%lf", &D ), D != -1 )
    { 
        double bsm, bsh, bmh, esm, esh, emh, begin, end, total = 0;
        // 计算第一次满足条件的时间
         bsm = D / s_m;
        bsh = D / s_h;
        bmh = D / m_h;
        // 计算第一次不满足条件的时间
         esm = ( 360 - D ) / s_m; // = tsm - bsm
        esh = ( 360 - D ) / s_h; // = tsh - bsh
        emh = ( 360 - D ) / m_h; // = tmh - bmh
        
        // 三重循环找出所有的时间总和,当然这三重循环的顺序是可以打乱的 
        for( double bt3 = bsh, et3 = esh; et3 <= 43200.000001; bt3 += tsh, et3 += tsh )
        {
            for( double bt2 = bmh, et2 = emh; et2 <= 43200.000001; bt2 += tmh, et2 += tmh )
            {
                if( et2 < bt3 )  // 这是为了提前判断是否有交集
                       continue;
                if( bt2 > et3 )
                    break;
                for( double bt1 = bsm, et1 = esm; et1 <= 43200.000001; bt1 += tsm, et1 += tsm )
                {
                    if( et1 < bt2 || et1 < bt3 )
                        continue;
                    if( bt1 > et2 || bt1 > et3 )
                        break;
                    begin = MAX( bt1, bt2, bt3 ); // 取它们的交集 
                       end = MIN( et1, et2, et3 );
                    total += ( end - begin );
                }
            }   
        }
        printf( "%.3lf\n", total / 432 );
    }        
}

写的好啊,我是fw

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值