时针分针秒针夹角问题

来源:互联网 发布:java中try catch的用法 编辑:程序博客网 时间:2024/04/29 16:38

Tick and Tick

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20103    Accepted Submission(s): 5255


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
012090-1
 

Sample Output
100.0000.0006.251
 题目意思是求分针时针秒针之间成的角度大于某个值时在一天的时间中所占的比例。
#include<stdio.h>#include<iostream>double MAX(double a,double b,double c){double max=a;if(b>=max)max=b;if(c>=max)max=c;return max;}double MIN(double x,double y,double z){double min=x;if(y<=min)min=y;if(z<=min)min=z;return min;}double TIME=43200.000001;//一天的秒数 //计算出各自的角速度double hds=30,mds=360,sds=120*180;//单位是 度/小时//计算之间的相对角速度 (角速度之差) 单位是度/分钟, double h_m=11.0/120,h_s=719.0/120,m_s=59.0/10; //由相对角速度计算每隔多长时间他们之间的夹角出现相同的情况double  wh_m=360.0/h_m,wh_s=360.0/h_s,wm_s=360.0/m_s;int main(){double D;double begin,end;while(scanf("%lf",&D),D!=-1){double anstime=0;//计算各自第一次满足条件的时间double sth_m=D/h_m,sth_s=D/h_s,stm_s=D/m_s;//计算各自第一次不能满足条件的时间double eth_m=(360-D)/h_m,eth_s=(360-D)/h_s,etm_s=(360-D)/m_s;//开始三重循环,求出每一次满足条件的开始时间和结束时间之差,并且累加起来,就可以求得总时间for(double st1=sth_m,et1=eth_m;et1<TIME;st1+=wh_m,et1+=wh_m){for(double st2=sth_s,et2=eth_s;et2<TIME;st2+=wh_s,et2+=wh_s){if(st1>et2)//h_m还没开始,h_s就结束了 continue;if(et1<st2)//h_s还没开始,h_m就结束了break;for(double st3=stm_s,et3=etm_s=etm_s;et3<TIME;st3+=wm_s,et3+=wm_s){if(st2>et3||st1>et3)//m_s结束了,其他还没开始 continue; if(et2<st3||et1<st3)//其他结束了,m_s还没开始 break;begin=MAX(st1,st2,st3);end=MIN(et1,et2,et3);anstime+=(end-begin);  } } }  printf("%.3lf\n",anstime/432);} }