本文共 4966 字,大约阅读时间需要 16 分钟。
1、
2、题目大意:
一个钟的三个指针在不停的转动,他们已经厌烦了这样,当他们互相的距离角度大于等于D时,他们会很开心,问一天之中他们happy的时间占总时间的概率。3、题目:
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其中的合并取交集参考网上代码,自己写的错了#include using namespace std;#define INF 0x7fffffffdouble 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#include using namespace std;#define INF 0x7fffffffdouble 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
转载地址:http://ahddi.baihongyu.com/