HDU 1006区间取交

3 篇文章 0 订阅

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1006
Tick and Tick

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

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

#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 p;
    if(a>0)
    {
        p.l=(D-b)/a;
        p.r=(360-D-b)/a;
    }
    else
    {
        p.l=(360-D-b)/a;
        p.r=(D-b)/a;
    }
    if(p.l<0)p.l=0;
    if(p.r>60)p.r=60;
    if(p.l>=p.r)  p.l=p.r=0;
    return p;
}
qujian jiao(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;
}
/*
   hh=30*h+m/2+s/120;
   mm=6*m+s/10;
   ss=6*s;
   D<=|hh-mm|<=360-D;
   D<=|hh-ss|<=360-D;
   D<=|mm-ss|<=360-D;
   hh-mm=
*/
double happytime(int h,int m)//计算 h 时,m 分 满足题意的秒数
{
    double a,b;
    qujian s[3][2];
    qujian s1;

    //解方程 D<=|hh-mm|<=360-D
    //hh=30*h+m/2+s/120;
    //mm=6*m+s/10;
    a=1.0/120-0.1;
    b=30*h+m/2.0-6*m;
    s[0][0]=solve(a,b);
    s[0][1]=solve(-a,-b);

    //解方程  D<=|hh-ss|<=360-D
    //hh=30*h+m/2+s/120;
    //ss=6*s;
    a=1.0/120-6.0;
    b=30*h+m/2.0;
    s[1][0]=solve(a,b);
    s[1][1]=solve(-a,-b);

    //解方程  D<=|mm-ss|<=360-D
    //mm=6*m+s/10;
    //ss=6*s;
    a=0.1-6;
    b=6*m;
    s[2][0]=solve(a,b);
    s[2][1]=solve(-a,-b);

    double res=0;
    //六个区间,选三个取交集。
    //因为绝对值的式子得到的两个区间要并,而三个不同表达式的区间要交,故这样做。
    for(int i=0;i<2;i++)
      for(int j=0;j<2;j++)
         for(int k=0;k<2;k++)
         {
             s1=jiao(jiao(s[0][i],s[1][j]),s[2][k]);
             res+=s1.r-s1.l;
         }
    return res;
}

int main()
{
    while(scanf("%lf",&D))
    {
        if(D==-1) break;
        double res=0;
        int h,m;
        for(h=0;h<12;h++)
        {
            for(m=0;m<60;m++)
            {
                res+=happytime(h,m);
            }
        }
        printf("%.3lf\n",res*100.0/(60*60*12));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值