HDU 5705

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(0≤HH<12,0≤MM<60,0≤SS<60).
The second line contains one integer a(0≤a≤180).

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

题目大概意思就是,时间为12小时制,给你一个时间,hh:mm:ss
告诉你一个时刻,让你输出在这个时刻之后的下一个时刻,
满足:该时刻,时针分针掐好相差某个的角度为a。
(注意,满足要求的时刻不一定是恰好以秒为单位,可以是秒与秒之间的时刻,我们可以向下取整)

我钟表问题一直都不太好,所以看了一下大佬的博客再加上自己思考的
附上连接添加链接描述
从最简单的开始
钟表钟12到3是一个直角90°,中间由三个30°组成,

5min = 30°,1min = 6°,所以分针走1°需要10s,同理时针走1°需要120s;
在相同的时间内120s内,分针跟时针相差11°,所以分针和时针相差1°就需要120/11s。
可以清楚的看到 120s相差11°

因为精度问题,可以通分解决这个问题,每个都乘上11,因为之前乘上了11,所以最后需要除以11

代码如下

/*The input contains multiple test cases.

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

0:59:59
30
01:00:00
30
*/

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    int hh, mm, ss;
    int ans = 0;// 角度
    while (~scanf("%d:%d:%d", &hh, &mm, &ss))
    {
        int ang;
        ans ++;
        cin >> ang;
        int T = (hh * 60 * 60 + mm * 60 + ss) * 11;
        ang *= 11;
        int Thm = 0, Tang = 0, i; // 时针和分针总相差多少度,  一圈内角度相差多少
        for (i = 120; ; i += 120)  //每120s后
        {
            Thm += 11;      // 时针和分针的角度都差11°
            if (Thm > 360 * 11)   // 走过了好几圈
                Tang = Thm % (11 * 360);  // 取余变成一圈内时针和分针相差几度
            else
                Tang = Thm;  // 一圈内时针和分针相差几度就是几度
            if(Tang > 180 * 11) // 一圈内相差超过半圈
                Tang = 360 * 11 - Tang;   // 取反向小的夹角
            if(Tang == ang && i > T)  // 如果达到了要求的角度并且 时间大于原本的时间秒数跳出循环输出
                break;
        }
        i %= 12 * 60 *60 * 11; // i是总秒数 取余12h制的转化为秒 为最简
        int HH = i / (3600 * 11 );
        int MM = (i - HH * (3600 * 11)) / (60 * 11);
        int SS = (i - HH * (3600 * 11) - MM * (60 * 11)) / 11;
        if(HH == 24) HH = 0;

        printf("Case #%d: %02d:%02d:%02d\n", ans, HH, MM, SS);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值