hdu1209 Clock sort思维题

Clock

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


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
 

很有意思的一道题,关键在于求时针和分针角度,刚开始犯了一个错误 就是以为时针是正好对着数字的,其实不是的,分钟在时针上也有体现,大概是0.5度一分钟

#include <stdio.h>  
#include <algorithm>  
#include <math.h>  
using namespace std;  
  
struct Node  
{  
    int h,m;  
    double r;  
}clock[10000];  
  
int cmp(Node x,Node y)  //Ties are broken in such a way that an earlier time precedes a later time.
{  						//角度相等,时间早的优先 
    if(x.r!=y.r)  
    return x.r<y.r;  
    return x.h<y.h;  
}  
  
int main()  
{  
    int t;  
    scanf("%d",&t);  
    while(t--)  
    {  
        int i;  
        for(i = 0;i<5;i++)  
        {  
            scanf("%d:%d",&clock[i].h,&clock[i].m);  
            if(clock[i].h>12)  
            clock[i].r = fabs(30.0*(clock[i].h-12)+clock[i].m/2.0-6.0*clock[i].m);  
            else  
            clock[i].r = fabs(30.0*clock[i].h+clock[i].m/2.0-6.0*clock[i].m);  
            if(clock[i].r>180)  
            clock[i].r = 360-clock[i].r;  
        }  
        sort(clock,clock+5,cmp);  
        printf("%02d:%02d\n",clock[2].h,clock[2].m);  
    }  
  
    return 0;  
}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值