HDU 1006.Tick and Tick【11月12】【很久没写博客了】

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
题意很简单,时钟三个指针之间的夹角大于D的时间在一天中所占的比例。具体思路看代码很清晰:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
double MAX(double a, double b, double c)
{
    if(a >= b && a >= c) return a;
    else if(b >= c) return b;
    else return c;
}
double MIN(double a, double b, double c)
{
    if(a <= b && a <= c) return a;
    else if(b <= c) return b;
    else return c;
}
int main()
{
    double s = 360. / 60., m = 360. / 3600., h = 360. / 43200.;//角速度
    double m_s = s - m, h_m = m - h, h_s = s - h;//相对较速度
    //cout << m_s <<" "<< h_m <<" "<< h_s << endl;
    double D,x_m_s,x_h_s,x_h_m,y_m_s,y_h_s,y_h_m;//周期
    double cycle_m_s = 360 / m_s;
    double cycle_h_m = 360 / h_m;
    double cycle_h_s = 360 / h_s;
    //cout << cycle_m_s <<" "<< cycle_h_m <<" "<< cycle_h_s << endl;
    while(scanf("%lf",&D) == 1 && D != -1)
    {
        //cout << D << endl;
        x_m_s = D / m_s;//周期内合法区间
        x_h_m = D / h_m;
        x_h_s = D / h_s;
        y_m_s = (360 - D) / m_s;
        y_h_m = (360 - D) / h_m;
        y_h_s = (360 - D) / h_s;
        //cout << x_m_s <<" "<< y_m_s << endl;
        //cout << x_h_m <<" "<< y_h_m << endl;
        //cout << x_h_s <<" "<< y_h_s << endl;
        double sumtime = 0;
        double begintime, endtime;

        //寻找交集
        for(double begin1 = x_m_s,end1 = y_m_s; end1 < 43200.000001; begin1 += cycle_m_s,end1 += cycle_m_s)
        {
            for(double begin2 = x_h_m,end2 = y_h_m; end2 < 43200.000001; begin2 += cycle_h_m,end2 += cycle_h_m)
            {
                if(end1 < begin2) break;
                if(end2 < begin1) continue;
                for(double begin3 = x_h_s,end3 = y_h_s; end3 < 43200.000001; begin3 += cycle_h_s,end3 += cycle_h_s)
                {
                    if(begin3 > end1 || begin3 > end2) break;
                    if(begin1 > end3 || begin2 > end3) continue;
                    begintime = MAX(begin1, begin2, begin3);
                    endtime = MIN(end1, end2, end3);
                    if(endtime > begintime) sumtime += (endtime - begintime);
                }
            }
        }
        printf("%.3f\n", sumtime / 432);
    }
    return 0;
}

啊····好久没写博客了····

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值