hdu1006

来源:互联网 发布:剑三明教成女捏脸数据 编辑:程序博客网 时间:2024/06/15 00:04

Tick and Tick

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


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题意就是说给你一个角度,一天之中三个指针相互的夹角都比这个角度大的几率;直接我就用1,1/60,1/3600来表示他们的角速度了,我想的是让他们同时开始走,应该不会出错的但是写完发现精度是有问题的,输出和答案有一定误差,所以看了下别人的代码,的确是精度有限制,所以改了一下,就过了,代码有详细注释!
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;double maxx(double a,double b,double c){    double temp=a>b?a:b;    temp=temp>c?temp:c;    return temp;}double minn(double a,double b,double c){    double temp=b>a?a:b;    temp=c>temp?temp:c;    return temp;}int main(){    int t;    double a[3],b[3],m[3],n[3];    while(scanf("%d",&t)!=EOF&&t!=-1)    {         double ans=0;         double sm=6.0-0.1;//秒针相对分钟的角速度         double mh=0.1-0.1/12.0;//分针相对时针的角速度         double sh=6.0-0.1/12;//秒针相对时针的角速度         double t1=360.0/sm;         double t2=360.0/mh;         double t3=360.0/sh;//运动周期          a[0]=t*1.0/sm;          a[1]=t*1.0/mh;          a[2]=t*1.0/sh;//会相交周期          b[0]=(360.0-t)/sm;          b[1]=(360.0-t)/mh;          b[2]=(360.0-t)/sh;//不会相交周期         for( m[0]=a[0],n[0]=b[0];m[0]<=43200.0;m[0]+=t1,n[0]+=t1)//12个小时是43200秒,因为一天两个12个小时的过程一定是一样的(在钟表上)            for( m[1]=a[1],n[1]=b[1];m[1]<=43200.0;m[1]+=t2,n[1]+=t2)             {               if(n[1]<m[0])                continue;//目前不会相交区间还不满足要求               if(m[1]>n[0])                break;//如果不会相交区间比会相交区域都小!                for( m[2]=a[2],n[2]=b[2];m[2]<=43200.0;m[2]+=t3,n[2]+=t3)                {                    if(n[2]<m[0]||n[2]<m[1])                        continue;//同上                    if(m[2]>n[0]||m[2]>n[1])                        break;//同上                    double mi=minn(n[0],n[1],n[2]);                    double ma=maxx(m[0],m[1],m[2]);//求交集自然是集合坐标最大到右边最小                    if(mi>ma)                        ans+=mi-ma;                }             }            printf("%.3lf\n",ans/432.0);    }    return 0;}


0 0
原创粉丝点击