HDU1006 tick and tick

Tick and Tick

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


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
 

Author
PAN, Minghao
 

Source
 

Recommend

JGShining


题目大意是时针分针秒针待在一起感觉不开心,要间隔至少D°才能感到开心,求他们一天之内开心的时间占得百分比。


一开始是使用的散点来求和求百分比,也就是一秒一秒的计算,然后除以43200【12小时的秒数】,但是出现了精度问题,也就是输入90的时候输出了6.225
在经过反复思量之后觉得思路应该是没有问题的,那么问题就应该出现在除法的精度上面。时针分针秒针应该按照连续的转动来计算而不是散点。
据说如果化为毫秒可以满足要求,但是会超时

然后翻了大神的代码,知道应该使用角速度来计算,然后再度开始重新编写
题目的整体思路大约是计算出来时针,分针,秒针的角速度,然后求相对速度,求得满足最大距离大于degree的区间,最后求交集

先贴一下原来的代码,WA的
#include<iostream>
#include<cstdio>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
    int n;
    float h,m,s,hm,ms,sh,sum;
    while(scanf("%d",&n)&&n!=-1)
    {
        sum = 0;
        for(int i = 0; i < 12; i ++)
        {
            for(int j = 0; j < 60; j ++)
            {
                for(int k = 0; k < 60; k ++)
                {
                    s = k*6;
                    m = j*6+k*0.1;
                    h = i*30+j*0.5+k*0.5/60;
                    hm = fabs(h-m);
                    ms = fabs(m-s);
                    sh = fabs(s-h);
                    hm = min(fabs(h-m),360-fabs(h-m));
                    ms = min(fabs(m-s),360-fabs(m-s));
                    sh = min(fabs(s-h),360-fabs(s-h));
                    if(hm >= n && ms >= n && sh >= n)
                        sum++;
                }
            }
        }
        sum = sum/432.0;
        cout << fixed << setprecision(3) << sum << endl;
    }
    return 0;
}

然后贴一下成功AC了的代码

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cmath>
using namespace std;
struct interval
{
    double b;
    double e;
};
int main()
{
    double ans,h,m,s;
    interval hm[11],hs[719],ms[709];
    double degree;
    while(1)
    {
    	ans = 0;
    	scanf("%lf",°ree);
		if(degree == -1.0)break;
        for(int i = 0; i < 11; i ++)
        {
            hm[i].b = (360*i+degree)/11.0*120;
            hm[i].e = (360*i+360-degree)/11.0*120;
        }
        for(int j = 0; j < 708; j ++)
        {
            ms[j].b = (360*j+degree)/5.9;
            ms[j].e = (360*j+360-degree)/5.9;
        }
        for(int k = 0; k < 719; k ++)
        {
            hs[k].b = (360*k+degree)/719.0*120;
            hs[k].e = (360*k+360-degree)/719.0*120;
        }
        for(int i = 0; i < 11; i ++)
        {
            for(int j = 0; j < 708; j ++)
            {
                if(hm[i].e > ms[j].b && hm[i].b < ms[j].e){
                for(int k = 0; k < 719; k ++)
                {
                    if(ms[j].e > hs[k].b && ms[j].b < hs[k].e)
                    {
                    	if(hm[i].b < hs[k].e && hm[i].e > hs[k].b)
                    	{
                    		double maxs = min(min(hs[k].e,ms[j].e),hm[i].e);
                    		double mins = max(max(hs[k].b,ms[j].b),hm[i].b);
                    		//cout << maxs << " " << mins << endl;
                    		ans = ans + maxs - mins;
						}
					}
                }
            	}
            }
        }
        cout << fixed << setprecision(3) << ans / 432.0 << endl;
    }
    return 0;
}

贴一些参考过的博客,致谢
语言分别是
C++
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值