zoj 2106 Tick and Tick(比较好的数学题目,代码特麻烦,注意精度)

1、http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1106

2、题目大意:

一个钟的三个指针在不停的转动,他们已经厌烦了这样,当他们互相的距离角度大于等于D时,他们会很开心,问一天之中他们happy的时间占总时间的概率。
我觉得这是在解不等式,我原来使用的暴力破解,毫无疑问失败了;我们只要找到某一分钟内,他们happy的时间,然后钟每过12个小时相当于43200秒复原一次。因此总时间就是43200白秒,只要求出在这43200的happy时间,答案就知道了;
假设现在的时钟是h小时,m分钟,s秒,给定的角度为degree;则列出happy的不等式有
时针到0时刻的角度的实际值为hw = (h+m/60+s/3600)*30,分针的角度mw = (m+s/60)*6,秒针的角度为sw = s*6;则他们都happy的条件为
degree<|hw -mw|<360 - degree;<1>
degree<|hw - sw|<360 - defgree;<2>
degree<|sw - mw|<360 - degree ;<3>
解这三个不等式即可得到s的区间,把区间的最大值减去最小值就是happy的时间,把每小时每分钟的happy时间再叠加,就是总的happy时间了,再除以总时间的百分比并保留三个小数就是答案。

3、题目:

Tick and Tick

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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


4、AC代码:

#include<stdio.h>
#include<algorithm>
using namespace std;
#define INF 0x7fffffff
double D;
struct node
{
    double l;
    double r;
}s[3][2];
node interval(double a,double b)//解方程D<=|a*s-b|<=360-D
{
    node 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>=p.r)
    {
        p.l=0;
        p.r=0;
    }
    if(p.l<0)
    p.l=0;
    if(p.r>60)
    p.r=60;
    return p;
}

node jiao(node a,node b)
{
    node 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 solve(int h,int m)
{
    double a,b;
    /*
      时针与分针的角度处理
      解方程式 D<=|hw-mw|<=360-D
               hw=(h+m/60+s/3600)*30
               mw=(m+s/60)*6
    */
    a=1.0/120.0-1.0/10.0;
    b=30*h+m/2.0-6.0*m;
    s[0][0]=interval(a,b);
    s[0][1]=interval(-a,-b);
    /*
      时针与秒针的角度处理
      解方程式 D<=|hw-sw|<=360-D
               hw=(h+m/60+s/3600)*30
               sw=s*6
    */
    a=1.0/120-6.0;
    b=30*h+m/2.0;
    s[1][0]=interval(a,b);
    s[1][1]=interval(-a,-b);
     /*
      分针与秒针的角度处理
      解方程式 D<=|mw-sw|<=360-D
               mw=(m+s/3600)*30
               sw=s*6
    */
    a=0.1-6;
    b=6*m;
    s[2][0]=interval(a,b);
    s[2][1]=interval(-a,-b);
    //两个绝对值出来的区间取并集,三个并集之间取交集
    node s1;
    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)!=EOF)
    {
        if(D==-1)
        break;
        double ans=0;
        for(int i=0;i<12;i++)
        {
            for(int j=0;j<60;j++)
            ans+=solve(i,j);//i小时j分钟
        }
        printf("%.3f\n",100.0*ans/(60*60*12));
    }
    return 0;
}

其中的合并取交集参考网上代码,自己写的错了

错的代码:

#include<stdio.h>
#include<algorithm>
using namespace std;
#define INF 0x7fffffff
double D;
struct node
{
    double l;
    double r;
}s[3][2];
node interval(double a,double b)//解方程D<=|a*s-b|<=360-D
{
    node 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>=p.r)
    {
        p.l=0;
        p.r=0;
    }
    if(p.l<0)
    p.l=0;
    if(p.r>60)
    p.r=60;
    return p;
}
node jiao(node a,node b)
{
    node 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 solve(int h,int m)
{
    double a,b;
    /*
      时针与分针的角度处理
      解方程式 D<=|hw-mw|<=360-D
               hw=(h+m/60+s/3600)*30
               mw=(m+s/60)*6
    */
    a=1.0/120-1.0/10.0;
    b=30*h+m/2.0-6.0*m;
    s[0][0]=interval(a,b);
    s[0][1]=interval(-a,-b);
    /*
      时针与秒针的角度处理
      解方程式 D<=|hw-sw|<=360-D
               hw=(h+m/60+s/3600)*30
               sw=s*6
    */
    a=1.0/120-6;
    b=30*h+m/2.0;
    s[1][0]=interval(a,b);
    s[1][1]=interval(-a,-b);
     /*
      分针与秒针的角度处理
      解方程式 D<=|mw-sw|<=360-D
               mw=(m+s/3600)*30
               sw=s*6
    */
     a=0.1-6;
     b=6.0*m;
    s[2][0]=interval(a,b);
    s[2][1]=interval(-a,-b);
    //两个绝对值出来的区间取并集,三个并集之间取交集

    node s1[20];
    int k=0;
    for(int i=0;i<3;i++)
    {
        if((s[i][0].r>=s[i][1].l) || (s[i][1].r>=s[i][0].l) )
        {
            s1[k].l=max(s[i][0].l,s[i][1].l);
            s1[k].r=max(s[i][0].r,s[i][1].r);
            k++;
        }
        else if(s[i][0].l>=s[i][1].l && s[i][0].r<=s[i][1].r)
        {
            s1[k]=s[i][1];
            k++;
        }
        else if(s[i][1].l>=s[i][0].l && s[i][1].r<=s[i][0].r)
        {
            s1[k]=s[i][0];
            k++;
        }
        else
        {
            s1[k]=s[i][0];
            k++;
            s1[k]=s[i][1];
            k++;
        }
    }
    node s2=s1[0];
    for(int i=1;i<k;i++)
    {
        s2=jiao(s2,s1[i]);
    }
    double anss;
    anss=s2.r-s2.l;
    return anss;
}
int main()
{
    while(scanf("%lf",&D)!=EOF)
    {
        if(D==-1)
        break;
        double ans=0;
        for(int i=0;i<12;i++)
        {
            for(int j=0;j<60;j++)
            ans+=solve(i,j);//i小时j分钟
        }
        printf("%.3f\n",100.0*ans/(60*60*12));
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值