zoukankan      html  css  js  c++  java
  • HDU1006

    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,求符合这个角度的一天中的happy时间占一天时间的百分之多少.....
    happy时间是指  时针,分针,秒针三者之间的角度大小满足>=D就是happy时间....
     
    思路:   一天24小时,也就是时钟转两圈。那么就求12小时的happy时间在除以12*60*60
        12小时的happy时间等于符合条件的每分钟的happy时间相加。
        接下来就是想办法求1分钟中符合条件的happy时间
        
             时针    分针    秒针
    每秒角速度   1/120         1/10           6
    一度所用时间      120             10             1/6
     
         假设现在的时间为h小时,m分钟,s秒。以数字12为起点。  
        则hw时针角度为:(h+m/60+s/3600)*1/120*3600  
         mw分针角度为:(m+s/60)*1/10*60
         sw秒针角度为:s*6
     
        要是happy时间就要满足下面三个条件
        D<=|hw-mw|<=360-D
        D<=|hw-sw|<=360-D
        D<=|mw-sw|<=360-D
        求得一个形式为D<=|ax+b|<=360-D   这里的x是秒
     
        就出来3个区间然后与[0.60]取交集得到h与m,m与s,h与s的三个两两指针的happy区间  最后在求这三个区间的交集就是符合条件的happy时间
     
     
    代码如下:(详情  可以参考kuangbin的博客园,我也是看他的才知道的)      http://www.cnblogs.com/kuangbin/archive/2011/12/04/2275470.html
    #include <stdio.h>
    #include <math.h>
    #include<iostream>
    #include <algorithm>
    using namespace std;
    struct qujian
    {
        double l,r;
    };
    double D;
    
    qujian solve(double a,double b)   //解方程 D<=a*x+b<=360-D ,并且和 [0,60] 取交集
    {
        qujian p1;
        if(a>0)
        {
            p1.l=(D-b)/a;
            p1.r=(360-D-b)/a;
        }
        else
        {
            p1.l=(360-D-b)/a;
            p1.r=(D-b)/a;
        }
        if(p1.l<0)
            p1.l=0;
        if(p1.r>60)
            p1.r=60;
        if(p1.l>=p1.r)
            p1.l=p1.r=0;
        return p1;
    }
    qujian compare(qujian a,qujian b)
    {
        qujian p;
        p.l=max(a.l,b.l);
        p.r=min(a.r,b.r);
        if(p.l>p.r)
            p.l=p.r=0;
        return p;
    }
    
    
    
    
    double happytime(double h,double m)
    {
        double a,b;
        int i,j,k;
        qujian s[3][2],p2;      //解方程D<=|hh-mm|<=360-D
        a=1.0/120.0-0.1;        //hh=30*h+m/2+s/120;
        b=30*h+m/2.0-6*m;
        s[0][0]=solve(a,b);
        s[0][1]=solve(-a,-b);
    
        a=0.1-6;
        b=6*m;
        s[1][0]=solve(a,b);
        s[1][1]=solve(-a,-b);
    
        a=1.0/120-6;
        b=30*h+m/2.0;
        s[2][0]=solve(a,b);
        s[2][1]=solve(-a,-b);
    
        double ans=0;
        for(i=0; i<2; i++)
        {
            for(j=0; j<2; j++)
                for(k=0; k<2; k++)
                {
                    p2=compare(compare(s[0][i],s[1][j]),s[2][k]);
                    ans+=p2.r-p2.l;
                }
        }
        return ans;
    }
    
    int main()
    {
        while(scanf("%lf",&D))
        {
            if(D==-1)
                break;
            double h,m,ans=0;
            for(h=0; h<12; h++)
            {
                for(m=0; m<60; m++)
                    ans+=happytime(h,m);
            }
            printf("%.3lf
    ",ans*100.0/(60*60*12));
        }
        return 0;
    }
  • 相关阅读:
    阻塞赋值和非阻塞赋值
    组合逻辑和时序逻辑
    信道估计常用算法
    Verilog有限状态机FSM
    希尔伯特变换
    微信小程序取消分享的两种方式
    orm 常用字段
    drf获取请求过来时的request
    WeChat--API
    Django之admin源码浅析
  • 原文地址:https://www.cnblogs.com/huangguodong/p/5553939.html
Copyright © 2011-2022 走看看