zoukankan      html  css  js  c++  java
  • 1006 Tick and Tick

     

    1006题:

    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.我们只需算出时钟从12点整(3个指针重合),到下一次12点整(3个指针再次重合)这12个小时的时间过程中,这3个指针之间的区间间隔大于制定角度的时间(即秒数)即可。

    2.既然是算秒数,我们可以算这12个小时,也就是在720分钟当中,每分钟有多少秒这3个指针的位置符合要求。

    3.于是,我们计算在h:m:s(h时m分s秒)时刻,这3个指针偏离12点整位置的角度(因为这样便于计算这3个指针的相对角度差,即它们相距的区间):

    首先:这3个指针的角速度:

    1. #define V_SEC 6.0       //秒针角速度:6度每秒
    2. #define V_MIN 0.1       //分针角速度:0.1度每秒  
    3. #define V_HOU 1.0/120   //时针角速度:1/120度每秒

    然后:这3个指针偏离0点的角度:

    1. #define A_SEC s*6                   //秒针角度:6*s  
    2. #define A_MIN m*6+s*0.1             //分针角度:0.1度*60s*m分钟+0.1度*s秒  
    3. #define A_HOU h*30+m*0.5+s/120.0    //时针角度:1/120度*3600s*h小时+1/120度*60s*m分钟+1/120*s秒

    4.假如用户输入的区间间隔是Angle度,则符合要求的位置满足如下公式:

    1) Angle <= |hAngle-mAngle| <= 360-Angle

    2) Angle <= |hAngle-sAngle| <= 360-Angle

    3) Angle <= |mAngle-sAngle| <= 360-Angle

    这样,我们就可以根据上式算出在h时m分这一分钟之内,有多少秒使得这3个指针的位置符合要求。

    所以,在这一分钟内,满足上面3个不等式的时间(即秒数),为我们所求。

    5.这样我们就能得到3个s的区间,而这个区间必须与[0,60]取交集,因为这是计算的一分钟内的秒数,不可能超过60秒。

    7.对这3个区间求交集,即可得到所需要的秒数。

    *****难点主要是数学公式的推导和交并集的处理*****

    代码:

      1 /**
      2  * @date 2013-2
      3  * @name Tick and Tick
      4  * 用角速度
      5  */
      6 #include <cstdio>
      7 #include <math.h>
      8 #include <iostream>
      9 #include <algorithm>
     10 
     11 #define V_SEC 6.0       //秒针角速度
     12 #define V_MIN 0.1       //分针角速度
     13 #define V_HOU 1.0/120   //时针角速度
     14 
     15 #define A_SEC s*6                   //秒针角度
     16 #define A_MIN m*6+s*0.1             //分针角度
     17 #define A_HOU h*30+m*0.5+s/120.0    //时针角度
     18 
     19 using namespace std;
     20 struct interval{    //区间
     21     double l;       //left
     22     double r;       //right
     23 };
     24 double Angle;       //角度
     25 int s=0;            //秒数
     26 interval solve(double v,double a){//解方程
     27     //Angle<=v*t+a<=360-Angle;,并且和[0,60]取交集
     28     interval p;
     29     if(v>0){
     30         p.l=(Angle-a)/v;
     31         p.r=(360-Angle-a)/v;
     32     }
     33     else{
     34         p.l=(360-Angle-a)/v;
     35         p.r=(Angle-a)/v;
     36     }
     37     if(p.l< 0)p.l= 0;
     38     if(p.r>60)p.r=60;
     39     if(p.l>=p.r)p.l=p.r=0;
     40     return p;
     41 }
     42 interval jiao(interval a,interval b){
     43     interval p;
     44     p.l=max(a.l,b.l);
     45     p.r=min(a.r,b.r);
     46     if(p.l>=p.r)p.l=p.r=0;
     47     return p;
     48 }
     49 /*
     50 hh=30*h+m/2+s/120
     51 mm=6*m+s*0.1
     52 ss=6*s
     53 Angle<=|hh-mm|<=360-Angle
     54 Angle<=|hh-ss|<=360-Angle
     55 Angle<=|mm-ss|<=360-Angle
     56 */
     57 double happytime(int h,int m){//计算h时m分 满足题意的秒数
     58     double v_diff;//速度差
     59     double a_diff;//角度差
     60     interval s0[3][2];
     61     interval s1;
     62     /*解方程 Angle<=|hh-mm|<=360-Angle*/
     63     v_diff=V_HOU-V_MIN;
     64     a_diff=A_HOU-A_MIN;//时针分针夹角
     65     s0[0][0]=solve( v_diff, a_diff);
     66     s0[0][1]=solve(-v_diff,-a_diff);
     67     /*解方程 Angle<=|hh-ss|<=360-Angle*/
     68     v_diff=V_HOU-V_SEC;
     69     a_diff=A_HOU-A_SEC;//时针秒针夹角
     70     s0[1][0]=solve( v_diff, a_diff);
     71     s0[1][1]=solve(-v_diff,-a_diff);
     72     /*解方程 Angle<=|mm-ss|<=360-Angle*/
     73     v_diff=V_MIN-V_SEC;
     74     a_diff=A_MIN-A_SEC;//分针秒针夹角
     75     s0[2][0]=solve( v_diff, a_diff);
     76     s0[2][1]=solve(-v_diff,-a_diff);
     77     /*
     78     六个区间,选三个取交集
     79     因为绝对值的式子得到的两个区间要并,而三个不同表达式
     80     的区间要交,故这样做
     81     */
     82     double res=0;
     83     for(int i=0;i<2;i++)
     84     for(int j=0;j<2;j++)
     85     for(int k=0;k<2;k++){
     86         s1=jiao(jiao(s0[0][i],s0[1][j]),s0[2][k]);
     87         res+=s1.r-s1.l;
     88     }
     89     return res;
     90 }
     91 int main(){
     92     int h,m;
     93     while(scanf("%lf",&Angle)){
     94         if(Angle==-1)break;
     95         double res=0;
     96         for(h=0;h<12;h++)
     97             for(m=0;m<60;m++)
     98                 res+=happytime(h,m);
     99         printf("%.3lf
    ",res*100.0/43200);
    100     }
    101 }
  • 相关阅读:
    the configured user limit (128) on the number of inotify instances has been reached
    RabbitMQ Docker 单服务器集群
    webapi和GRPC性能对比
    camstart API 服务器负载均衡
    视图查询缺少值
    system.Data.Entity.Infrastructure.DbUpdateConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0) 问题
    WCF 基础连接已经关闭: 服务器关闭了本应保持活动状态的连接。
    优化sql用到的方法
    调用C++动态链接库出现错误
    ThoughtWorks.QRCode源码
  • 原文地址:https://www.cnblogs.com/ttzm/p/6029639.html
Copyright © 2011-2022 走看看