来源:http://acm.hdu.edu.cn/showproblem.php?pid=1006
看秒针的转动,一分钟一分钟的算
#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
struct range//取值范围
{
double l,r;
};
double d;
range work(double a,double b)//求 d<=ax+b<360-d 的解,x为秒。
{
range temp;
if(a>0)
{
temp.l=(d-b)/a;
temp.r=(360-d-b)/a;
}
else
{
temp.l=(360-d-b)/a;
temp.r=(d-b)/a;
}
if(temp.l<0)temp.l=0;
if(temp.r>60)temp.r=60;
if(temp.l>=temp.r)temp.r=temp.l=0;
return temp;
}
range same(range a,range b)//求并集
{
range temp;
temp.l=(a.l>b.l)?a.l:b.l;
temp.r=(a.r<b.r)?a.r:b.r;
if(temp.l>temp.r)temp.l=temp.r=0;
return temp;
}
int main()
{
int i,j,k,h,m;
double a1,a2,a3,b1,b2,b3,sum;
range temp[3][2],aa;
while(scanf("%lf",&d)&&d!=-1)
{
sum=0;
for(h=0;h<12;h++)
for(m=0;m<60;m++)
{
b1=6.0*m; a1=-5.9;
b2=30*h+0.5*m; a2=1.0/120-6.0;
b3=30*h+(0.5-6)*m; a3=(1.0/120)-0.1;
temp[0][0]=work(a1,b1);temp[0][1]=work(-a1,-b1);
temp[1][0]=work(a2,b2);temp[1][1]=work(-a2,-b2);
temp[2][0]=work(a3,b3);temp[2][1]=work(-a3,-b3);
for(i=0;i<2;i++)
for(j=0;j<2;j++)
for(k=0;k<2;k++)
{
aa=same(same(temp[0][i],temp[1][j]),temp[2][k]);
sum+=aa.r-aa.l;
}
}
sum=sum*100.0/(3600*12);
printf("%.3lf\n",sum);
}
return 0;
}