hdu1006 Tick and Tick 区间交集

/***********************************************************
题意:一个时钟,有三根指针,秒钟分钟和时钟,当每跟指针跟其他两根相差角度大于 D 时,为happy的时间,给定 D 求一天中有百分之多少的时间是happy的;
思路:两根两根的考虑,求出每个的区间,再把三组区间求交集
设两根针的相对速度为 v 度每秒,那么相差度数大于x则有 k * 360 + D < t * v < k * 360 + 360 - D;
那么区间就是[(k * 360 + D) / v, (k * 360 + 360 - D) / v]
************************************************************/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;

struct Endpoint
{
    double pos ;
    int k ;
}endpoint[60 * 12 * 6 + 10];

double s_m = 360.0 / 60.0        - 360.0 / 60.0 / 60.0;
double m_h = 360.0 / 60.0 / 60.0 - 360.0 / 60.0 / 60.0 / 12.0;
double s_h = 360.0 / 60.0        - 360.0 / 60.0 / 60.0 / 12.0;
double maxt = 60.0 * 60.0 * 12.0;
double D;
int numEndpoint;

bool cmp(Endpoint a, Endpoint b) { return a.pos < b.pos; }


void buildInterval(double v)
{
    int k = 0;
    while((k * 360.0 + D) / v <= maxt)
    {
        endpoint[numEndpoint].pos = (k * 360.0 + D) / v;
        endpoint[numEndpoint++].k = 1;
        endpoint[numEndpoint].pos = min(maxt, (k * 360.0 + 360.0 - D) / v);
        endpoint[numEndpoint++].k = -1;
        //printf("%d\n", numEndpoint);
        k++;
    }
}

double intersection()
{

    int in = 0, i, ok = 0;
    double ans = 0.0;
    double l;
    for(i=0; i < numEndpoint; i++)
    {
        in += endpoint[i].k;
        if(in == 3)
        {
            l=endpoint[i].pos;
            ok=1;
        }
        else if(ok)
        {
            ans += endpoint[i].pos - l;
            ok=0;
        }
    }
    return ans;
}


int main()
{
    while(scanf("%lf", &D), D > -0.1)
    {
        //printf("%f\n",D);
        numEndpoint = 0;
        buildInterval(s_m);
        buildInterval(m_h);
        buildInterval(s_h);
        sort(endpoint, endpoint + numEndpoint, cmp);
        //for(int i = 0; i < numEndpoint; i++) printf("i=%d   %f  %d\n", i, endpoint[i].pos, endpoint[i].k);
        printf("%.3f\n", intersection() / maxt * 100.0);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值