hdu 5705 Clock

Clock

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1020    Accepted Submission(s): 362


Problem Description
Given a time HH:MM:SS and one parameter  a , you need to calculate next time satisfying following conditions:

1. The angle formed by the hour hand and the minute hand is  a .
2. The time may not be a integer(e.g. 12:34:56.78), rounded down(the previous example 12:34:56).

 

Input
The input contains multiple test cases.

Each test case contains two lines.
The first line is the time HH:MM:SS (0HH<12,0MM<60,0SS<60) .
The second line contains one integer  a(0a180) .
 

Output
For each test case, output a single line contains test case number and the answer HH:MM:SS.
 

Sample Input
  
  
0:59:59 30 01:00:00 30
 

Sample Output
  
  
Case #1: 01:00:00 Case #2: 01:10:54
这个模拟比赛的时候敲得心碎。。
写了半天丢给队友。。
独自郁闷。。
先看看比赛的代码。
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<algorithm>
using namespace std;
double judge(double x)
{
    if(x<0)
        return x+360;
    if(x>360)
        return x-360;
    return x;
}
int main()
{
    int h,m,s,Case = 1;
    double M_deg=(360.0/60)/60;///每秒分
    double H_deg=M_deg*1.0/12;///时针
    double Deg=M_deg-H_deg;///
    int D=0;
    //printf("%lf\n",Deg);
    while(~scanf("%d:%d:%d",&h,&m,&s))
    {
        scanf("%d",&D);
        double H=(s/3600.0+m/60.0+h)*(30.0);
        double M=(s/60.0+m)*(6.0);
        //printf("%f %f\n",H,M);
        double R,L;
        int res = 0;
        if(H>M)
        {
            //printf("$");
            if(H-M>D)
                res = (int)((H-M-D)/Deg);
            else if(fabs(H-M-D) <= 1e-9)
                res = (int)((D*2.0)/Deg);
            else
                res = (int)((H-M+D)/Deg);
        }
        else if(fabs(M-H) <= 1e-9)
        {
            //printf("&");
            res = (int)(D/Deg);
        }
        else if(H<M)
        {
            //printf("^");
            if(M-H>D)
            {
                res = (int)((360.0-D-(M-H))/Deg);
            }
            else if(fabs(M-H-D) <= 1e-9)
                res = (int)((360.0-D-D)/Deg);
            else
                res = (int)((D-(M-H))/Deg);
        }

        //printf("**%d\n",res);
        while(res--)
        {
            s++;
            if(s==60)
                s=0,m++;
            if(m==60)
                m=0,h++;
            if(h==12)
                h=0;
        }
        printf("Case #%d: %02d:%02d:%02d\n",Case++,h,m,s);
    }
}


膜一发大佬代码: 大佬链接

题意:给你一个时间和一个角度,问你下一个时针和分针形成给出的角度是什么时候

题解:我们可以将这个问题看成追及问题,考虑时针:每小时30°,每分钟1/2°,每秒1/120°,考虑分针:没分钟6°,每秒1/10°,如果我把全部同时乘一个120,不就能避免精度造成的问题了吗!现在再来考虑时针和分针的位置情况:这里我们以分针按顺时针的方向到时针


1:当时针和分针的θ∠大于180*120时(1)当(360*120-θ)大于给出的角度an时,需要的时间ans=h+(θ-an*120)/11,/11是因为每秒钟时针走1°,分针走12°。(2)当(360*120-θ)小于给出的角度an时,需要的时间ans=h+(θ-(360-an)*120)/11

2:当时针和分针的θ∠小于180*120时(1)当(360*120-θ)大于给出的角度an时,需要的时间ans=h+(θ-an*120)/11。(2)当(360*120-θ)小于给出的角度an时,需要的时间ans=h+(θ+an*120)/11。

最后再根据经过的时间算出当前的时间

[cpp]  view plain  copy
  1. #include<cstdio>  
  2. int hh,mm,ss,ic=1,h,m,s,an,d,ans,mo=120*360,mod=120*180;  
  3. void fuck(){  
  4.     s++,h=(3600*h+m*60+s)%mo,m=(m*720+12*s)%mo,d=(h+mo-m)%mo;  
  5.     if(d>=mod)if(an*120<mo-d)ans=h+(d-an*120)/11;else ans=h+(d-(360-an)*120)/11;  
  6.     else if(an*120<=d)ans=h+(d-an*120)/11;else ans=h+(d+an*120)/11;  
  7. }  
  8. int main(){  
  9.     while(~scanf("%d:%d:%d%d",&h,&m,&s,&an))  
  10.     fuck(),printf("Case #%d: %02d:%02d:%02d\n",ic++,ans/3600%12,ans/60%60,ans%60);  
  11.     return 0;  
  12. }  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值