首页 > 技术文章 > hdu5705

LJHAHA 2019-03-21 22:16 原文

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5705

题目:

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

题意:给出时间 HH:MM:SS 角度a 问下一个H和M的角度为a的时刻?

思路:

时针每小时30度,每分钟30/60度,每秒1/120 (3600,60,1)
分针每小时360,每分钟6度,每秒6/60,1/10度; (,720,12)避免精度问题全部乘以120 
算出h,m初始角度 h,m之间角度为deg=abs(h-m),暴力枚举几秒后deg==a即可,O(T)

参考:http://www.bubuko.com/infodetail-2051662.html

代码:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
const double eps=1e-3;
const int N=2e5+20;
int a;
int main()
{
    int cas=1;
    int h,m,s;
    while(~scanf("%d:%d:%d",&h,&m,&s))
    {
        cin>>a;
        a*=120;
        int sum=h*3600+m*60+s;//总的时间(秒) 
        //h,m初始角度 
        int hh=sum%(360*120);
        int mm=(sum*12)%(360*120);
        
        int ans=0;//暴力枚举几秒后
        while(true)
        {
            hh=(hh+1)%(360*120);//时针每秒1/120度,但由于乘了120,所以每次+1度
            mm=(mm+12)%(360*120);//分针每秒1/10度,但由于乘了120,所以分针1秒离散成12度
            ans++;
            int deg=abs(hh-mm);//h,m角度 
            if(abs(deg-a)<=10)//误差在1度内 
                break;
        }
        int anss=(ans+s)%60;
        int ansm=((ans+s)/60+m)%60;
        int ansh=(((ans+s)/60+m)/60+h)%12;
        printf("Case #%d: %02d:%02d:%02d\n",cas++,ansh,ansm,anss);                          
    }
    return 0;
}

 

推荐阅读