HDU-1006 JAVA+C++

3 篇文章 0 订阅

Tick and Tick
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26114 Accepted Submission(s): 7122

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

Author
PAN, Minghao

Source
ZJCPC2004
这一题花了我很长时间去理解,之后怎么也想不通只能去看大佬们的解释了
先缩短一半的时间:早上的12个小时和下午的12小时对时钟是一样的,因为时钟12小时与0小时的三针位置相同。接着就是了解到每次所有的针从有重合到再次有重合至多有一段连续的段符合三针分离度大于n。所以只要枚举每个重合到重合的时间段,然后求出每段中符合条件的时间段即可。

由于枚举的是重合到重合,所以先求出时针和分针,分针和秒针,时针和秒针之间的相对角速度和再重合周期。通过相对角速度hm,hs,ms,我们可以求出从重合到分离n度所需的时间n/hm,n/hs,n/ms,以及到再重合前n度所需的时间(360-n)/hm,(360-n)/hs,(360-n)/ms。每次枚举,i,j,k表示hm,hs,ms各自重合的时间,那么p=max(i+n/hm,j+n/hs,k+ms)就是最早的三针分离n度的时间;q=min(i+(360-n)/hm,j+(360-n)/hs,k+(360-n)/ms)就是最晚三针合并到n度的时间,那么时间区间[p,q]就是符合条件的时间段。
之后我用JAVA 以及C++都写了一遍
C++版本的,就是看大佬的嘿嘿嘿

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=12*60*60;
double hm,hs,ms,T_hm,T_hs,T_ms;
void init()
{
    double h,m,s;//角速度
    h=1.0/120;
    m=1.0/10;
    s=6;
    hm=m-h;hs=s-h;ms=s-m;//表示两个针分离的速度
    T_hm=360/hm;//表示时针和分针从重合到再次重合所花的时间,下面同理
    T_hs=360/hs;
    T_ms=360/ms;
    //printf("%.3lf %.3lf %.3lf\n",T_hm*11,T_hs*719,T_ms*59);
}
double Max(double a,double b,double c)
{
    return max(max(a,b),c);
}
double Min(double a,double b,double c)
{
    return min(min(a,b),c);
}
int main()
{
    init();
    double n;
    while(scanf("%lf",&n)!=EOF)
    {
        if(n<0)break;
        double i,j,k,a[6],p,q,ans=0;
        //两针分离到n所需的时间
        a[0]=n/hm;
        a[1]=n/hs;
        a[2]=n/ms;
        //两针合并到n所需的时间,等价于两针分离了360-n
        a[3]=(360-n)/hm;
        a[4]=(360-n)/hs;
        a[5]=(360-n)/ms;
        //每次所有的针从有重合到再次有重合至多有一段连续的段符合三针分离度大于n。
        for(i=0;i<=1.0*maxn;i+=T_hm)
        {
            for(j=0;j<=1.0*maxn;j+=T_hs)
            {
                if(j+a[1]>i+a[3])break;//p>=j+a[1]&&q<=i+a[3]=>p>q=>无效
                if(i+a[0]>j+a[4])continue;//与上同理
                for(k=0;k<=1.0*maxn;k+=T_ms)
                {
                    if(k+a[2]>i+a[3]||k+a[2]>j+a[4])break;
                    if(i+a[0]>k+a[5]||j+a[1]>k+a[5])continue;
                    p=Max(i+a[0],j+a[1],k+a[2]);//在这三个时间段刚好完成分离n度,所以取最大值才能保证全都分离n以上
                    q=Min(i+a[3],j+a[4],k+a[5]);//在这三个时间段刚好完成合并n度,所以取最小值才能保证全都未合并到n以内
                    if(q>p)
                        ans+=q-p;
                }
            }
        }
        printf("%.3lf\n",100.0*ans/maxn);
    }
    return 0;
}

之后用java写的

import java.util.Scanner;

public class Main{
    public static void main(String args[]) {
        Scanner sc=new Scanner(System.in);
        double h=1.0/120,m=1.0/10,s=6.0;
        double hm=m-h,hs=s-h,ms=s-m;
        double T_hm=360/hm,T_hs=360/hs,T_ms=360/ms;
        while(sc.hasNext()){
            double dec=sc.nextDouble();
            if(dec==-1)break;
            double a[]=new double[6];
            double ans=0;
            double p=0,q=0;
            a[0]=dec/hm;a[1]=dec/hs;a[2]=dec/ms;
            a[3]=(360-dec)/hm;a[4]=(360-dec)/hs;a[5]=(360-dec)/ms;
            for(double i=0;i<=43200.0;i+=T_hm){
                for(double j=0;j<=43200.0;j+=T_hs){
                    if((j+a[1])>(i+a[3]))break;
                    if((i+a[0])>(j+a[4]))continue;
                    for(double k=0;k<=43200.0;k+=T_ms){
                        if((k+a[2])>(i+a[3])||(k+a[2])>(j+a[4]))break;
                        if((i+a[0])>(k+a[5])||(j+a[1])>(k+a[5]))continue;
                        p=getMax(i+a[0],j+a[1],k+a[2]);
                        q=getMin(i+a[3],j+a[4],k+a[5]);
                        if(q>p){
                            ans+=q-p;
                        }
                    }
                }
            }
            System.out.printf("%.3f",ans*100.0/43200);
            System.out.println();
        }
        
    }
    public static double getMax(double hm,double hs,double ms){
        double Max=Math.max(Math.max(hm,hs), ms);
        return Max;
    }
    public static double getMin(double hm,double hs,double ms){
        double Min=Math.min(Math.min(hm,hs), ms);
        return Min;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值