标签:
#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 p1; if(a>0) { p1.l=(D-b)/a; p1.r=(360-D-b)/a; } else { p1.l=(360-D-b)/a; p1.r=(D-b)/a; } if(p1.l<0) p1.l=0; if(p1.r>60) p1.r=60; if(p1.l>=p1.r) p1.l=p1.r=0; return p1; } qujian compare(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; } double happytime(double h,double m) { double a,b; int i,j,k; qujian s[3][2],p2; //解方程D<=|hh-mm|<=360-D a=1.0/120.0-0.1; //hh=30*h+m/2+s/120; b=30*h+m/2.0-6*m; s[0][0]=solve(a,b); s[0][1]=solve(-a,-b); a=0.1-6; b=6*m; s[1][0]=solve(a,b); s[1][1]=solve(-a,-b); a=1.0/120-6; b=30*h+m/2.0; s[2][0]=solve(a,b); s[2][1]=solve(-a,-b); double ans=0; for(i=0; i<2; i++) { for(j=0; j<2; j++) for(k=0; k<2; k++) { p2=compare(compare(s[0][i],s[1][j]),s[2][k]); ans+=p2.r-p2.l; } } return ans; } int main() { while(scanf("%lf",&D)) { if(D==-1) break; double h,m,ans=0; for(h=0; h<12; h++) { for(m=0; m<60; m++) ans+=happytime(h,m); } printf("%.3lf\n",ans*100.0/(60*60*12)); } return 0; }
标签:
原文地址:http://www.cnblogs.com/huangguodong/p/5553939.html