Sicily 1182. Context-Free Clock

本文介绍了一种用于解决复杂时间计算问题的高效算法,该算法通过精确计算角度来确定时间,适用于多种时间计算场景,特别适用于钟表角度转换成实际时间的计算。

1182. Context-Free Clock

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

You recently installed a stylish clock in your office that is perfectly round and has no markings that identify its orientation. After accidentally bumping it, you realized that 12 o'clock might no longer be at the top. Nonetheless, you want to figure out what time it is. Fortunately you recently overheard A a coworker giving the time and you have a protractor and can measure the angle between the hour and minute hands.

 

Your program should print the first time that has the correct angle between the hour and minute hands and that is on or after the overheard time. The angle (0 to 359 degrees, inclusive) will be measured clockwise from the hour hand to the minute hand. Assume that the clock hands move smoothly.

Input

Input will consist of one test case per line, of the form


A HH :MM :SS


where A is the integral number of degrees that must be traversed clockwise to get from the hour hand to the minute hand and HH : MM : SS is the overheard time in 24 hour form. 0$ \le$A$ \le$359 , 0$ \le$HH$ \le$23 , 0$ \le$MM$ \le$59 , and 0$ \le$SS$ \le$59 . HH , MM , and SS will be exactly two digits with a leading zero if necessary.

End of input will be signaled by the line

-1 00:00:00

Output

Output will consist of one line per test case, of the form


HH :MM :SS


where HH : MM : SS is the first time on or after the input time where the angle from the hour hand to the minute hand is exactly A degrees, rounded down to the nearest second. HHMM , and SS should be zero padded to two digits and in the same range as the input ( 0...23 , 0...59 , and 0...59 respectively).

Sample Input

270 14:45:00 
0 12:00:00 
0 12:00:01 
300 13:30:00 
180 08:30:00 
-1 00:00:00

Sample Output

15:00:00 
12:00:00 
13:05:27 
14:00:00 
09:16:21
// Problem#: 1182
// Submission#: 3550619
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>

double degree[86400];

inline double abs(double a) {
    if (a < 0) return -a;
    return a;
}

void init() {
    double hourHandPos, minuteHandPos, differ;
    for (int i = 0; i < 24; i++) {
        for (int j = 0; j < 60; j++) {
            for (int k = 0; k < 60; k++) {
                hourHandPos = (i % 12) * 30 + (1.0 * j * 60 + k) / 120;
                minuteHandPos = j * 6 + 1.0 * k / 10;
                differ = hourHandPos > minuteHandPos ? 360 - hourHandPos + minuteHandPos : minuteHandPos - hourHandPos;
                degree[i * 3600 + j * 60 + k] = differ;
            }
        }
    }
    degree[86401] = 0;
}

double ERR = 0.08;

inline bool isSame(double d1, double d2) {
    if (d1 == 0) {
        return abs(d1 - d2) < ERR || abs(360.0 - d2) < ERR;
    }
    return abs(d1 - d2) < ERR;
}

int main() {

    init();

    while (1) {
        int h, m, s;
        double d;
        scanf("%lf %d:%d:%d", &d, &h, &m, &s);
        if (d == -1) break;
        int startPos = h * 3600 + m * 60 + s;
        bool found = false;
        int pos;
        for (pos = startPos; pos < 86400; ) {
            if (isSame(d, degree[pos])) break;
            pos++;
            if (pos == 86400) pos = 0;
        }
        printf("%02d:%02d:%02d\n", pos / 3600, (pos % 3600) / 60, pos % 3600 % 60);
    }

    return 0;
}                                 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值