# HDU 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;
}


#### [HDU] 5306 Gorgeous Sequence [区间取min&求和&求max][线段树]

2017-04-03 18:09:08

#### HDU－5700－区间交

2016-06-01 03:29:04

#### hdu 5306 区间取min和询问区间和及最大数

2016-05-29 11:33:59

#### 杭电ACM HDU 1006 Tick and Tick

2013-02-22 21:46:51

#### HDU:1006 Tick and Tick

2016-10-18 20:41:27

#### HDU 1006 Tick and Tick（ 模拟题）

2016-04-14 10:58:44

#### 杭电ACM1006 Tick and Tick

2015-11-30 19:42:43

#### [JAVA]杭电OJ1006

2014-05-08 16:56:35

#### 线段树区间取max黑科技

2016-07-07 16:50:51

#### 分治法 区间的价值 hdu5696

2016-05-23 01:27:25