[ACM_模拟] HDU 1006 Tick and Tick [时钟间隔角度问题]

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>
/*-----------------------------------------------
double取最大最小函数
*/
double max(double a,double b){
if(a>b)return a;
return b;
}
double min(double a,double b){
if(a>b)return b;
return a;
}//---------------------------------------------
/*
集合结构体
*/
struct set{
double a,double b;
};//集合区间的左右
double d;
/*---------------------------------------------
求d<=ax+b<360-d的解
*/
struct set sloveset(double a,double b){
struct set seta;
if(a>){
seta.a=(d-b)/a;
seta.b=(-d-b)/a;
}
else{
seta.b=(d-b)/a;
seta.a=(-d-b)/a;
}
if(seta.a<) seta.a=;
if(seta.b>) seta.b=;
if(seta.a>=seta.b) seta.a=seta.b=;//之前这句放到了if(seta.a<0)if(seta.b>60)前面了
return seta; //结果seta.b变成了负的怀疑是seta.b太大了冒了不知对错
}
/*---------------------------------------------
给2个集合求交集
*/
struct set intersection(struct set a,struct set b){
struct set p;
p.a=a.a>b.a ?a.a:b.a;
p.b=a.b<b.b ?a.b:b.b;
if(p.a>p.b) p.a=p.b=;
return p;
}//////////////////////////////////////////////////////////
int main(){
int h,m,i,j,k;
double a1,b1,a2,b2,a3,b3,time;
struct set answer[][],ensemble;
while(scanf("%lf",&d)&&d!=-){
time=;
for(h=;h<;h++){
for(m=;m<;m++){
b1=6.0*m;a1=-5.9;
b2=*h+0.5*m;a2=1.0/-6.0;
b3=*h+(0.5-)*m;a3=(1.0/)-0.1;
/*求3个绝对值不等式的解集 存到answer中answer[0][0] answer[0][1]要取并集剩下两个也是 */
answer[][]=sloveset(a1,b1); answer[][]=sloveset(-a1,-b1);
answer[][]=sloveset(a2,b2); answer[][]=sloveset(-a2,-b2);
answer[][]=sloveset(a3,b3); answer[][]=sloveset(-a3,-b3);
// 取过交集后,需要将3个式子的结果取并集 所以采用下面的方法
for(i=;i<;i++){
for(j=;j<;j++){
for(k=;k<;k++){
ensemble=intersection(intersection(answer[][i],answer[][j]),answer[][k]);
time+=ensemble.b-ensemble.a;
}
}
}
}
}
time=time*100.0/(*);
printf("%.3lf\n",time);
}
return ;
}
 
上一篇:Android KeyCode


下一篇:Emacs杂谈(一)Emacs环境 c++ 快捷键