hdu1209 Clock

简介: hdu1209 Clock

Clock

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6807    Accepted Submission(s): 2156


 

Problem Description

There is an analog clock with two hands: an hour hand and a minute hand. The two hands form an angle. The angle is measured as the smallest angle between the two hands. The angle between the two hands has a measure that is greater than or equal to 0 and less than or equal to 180 degrees.

Given a sequence of five distinct times written in the format hh : mm , where hh are two digits representing full hours (00 <= hh <= 23) and mm are two digits representing minutes (00 <= mm <= 59) , you are to write a program that finds the median, that is, the third element of the sorted sequence of times in a nondecreasing order of their associated angles. Ties are broken in such a way that an earlier time precedes a later time.

For example, suppose you are given a sequence (06:05, 07:10, 03:00, 21:00, 12:55) of times. Because the sorted sequence is (12:55, 03:00, 21:00, 06:05, 07:10), you are to report 21:00.

 

 

Input

The input consists of T test cases. The number of test cases (T) is given on the first line of the input file. Each test case is given on a single line, which contains a sequence of five distinct times, where times are given in the format hh : mm and are separated by a single space.

 

 

Output

Print exactly one line for each test case. The line is to contain the median in the format hh : mm of the times given. The following shows sample input and output for three test cases.

 

 

Sample Input

 

3 00:00 01:00 02:00 03:00 04:00 06:05 07:10 03:00 21:00 12:55 11:05 12:05 13:05 14:05 15:05

 

 

Sample Output

 

02:00 21:00 14:05

 

 

Source

Asia 2003(Seoul)

 

其实本题是一道简单的计算题:

时针:60分钟走一格,一格为360°/12=30°,所以时针的速度为30°/h,即0.5°/min

分针:60分钟走一圈,所以分针的速度为360°/60=6°/min

随后便可以根据其速度来解题了

代码如下:

#include<stdio.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct temp
{
    int h;
    int m;
    double angle;
}a[5];
int cmp(temp x,temp y)
{
    if(x.angle==y.angle)  //角度相同,时间晚的在前面
    {
        if(x.h==y.h)
            return x.m>y.m;
        return x.h>y.h;
    }
    return x.angle>y.angle;//角度小的在前面
}
int main()
{
    int T;
    scanf("%d",&T);
    getchar();
    while(T--)
    {
        double angle,angle_h,angle_m;
        int h,m,i;
        for(i=0;i<5;i++)
        {
            scanf("%d:%d",&h,&m);
            angle_h=30*(h%12)+0.5*m; //计算时针的角度
            angle_m=6*m;//计算分针的角度
            angle=abs(angle_h-angle_m);//夹角
            if(angle>180)  //超过180°的情况
                angle=360-angle;
            a[i].h=h;
            a[i].m=m;
            a[i].angle=angle;
            //printf("%02d:%d  %lf\n",a[i].h,a[i].m,a[i].angle);
        }
        sort(a,a+5,cmp);//排序
        printf("%02d:%02d\n",a[2].h,a[2].m); //按格式输出
    }
    return 0;
}
目录
相关文章
arm-linux-gcc的下载与安装步骤
arm-linux-gcc的下载与安装步骤
2086 2
|
传感器 调度 开发者
【Freertos基础入门】freertos任务的优先级
【Freertos基础入门】freertos任务的优先级
1753 0
|
数据可视化 C# C++
工业基础类IFC—提取模型结构树
工业基础类IFC—提取模型结构树
工业基础类IFC—提取模型结构树
|
编解码 前端开发 测试技术
这可能是市面上最好用的iOS云真机
最好用的iOS云真机,是怎么实现的呢?快来了解下吧!
3772 0
这可能是市面上最好用的iOS云真机
|
分布式计算 算法 大数据
大数据数据一致性
【10月更文挑战第24天】
291 4
|
9月前
|
存储 安全 Linux
CentOS 7.9系统备份:每日定期发送最新备份文件到另一台服务器。
注意,这个解决方案忽略了很多细节,例如错误处理和通知、备份版本控制、循环处理旧的备份文件等等。此外,你也应该尽量保持源服务器和目标服务器之间快速,稳定且安全的网络连接,并且目标服务器应该有足够的空间用于存放每天的备份文件。如果你需要更高级的备份解决方案,可能需要考虑一下使用专门的备份工具或者服务。
395 18
|
SQL JSON JavaScript
JavaWeb基础9——VUE,Element&整合Javaweb的商品管理系统
Vue 指令、生命周期、this和$、vue脚手架进行模块化开发/ElementUI框架、综合案例,element商品列表展示增删改查
JavaWeb基础9——VUE,Element&整合Javaweb的商品管理系统
|
安全 数据安全/隐私保护 CDN
阿里云海外视频安全的DRM
阿里云海外视频安全的DRM加密
|
自然语言处理 PyTorch 语音技术
Transformers 4.37 中文文档(八十)(4)
Transformers 4.37 中文文档(八十)
266 2
vscode——Prettier插件保存自动格式化
vscode——Prettier插件保存自动格式化
695 0