HDU 1209 Clock

Clock

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


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)
 

Recommend
mcqsmall
 

分析:时针一小时走30度,分针一分钟走6度,时针一分钟走0.5度,注意若角度相同,时间早的输出

代码:这里防止浮点误差,直接把度数乘以2计算

#include
#include
struct node{
    int h;
    int m;
    int angle;
}a[5];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int h,m,ang1,ang2,ang3;
        int i,j;
        for(i=0;i<5;i++)
        {
            scanf("%d:%d",&a[i].h,&a[i].m);
            ang1=(a[i].h)%12*60+a[i].m;
            ang2=(a[i].m)*12;
            ang3=(int)fabs(ang1-ang2);
            if(ang3>360)
                ang3=720-ang3;
            a[i].angle=ang3;
        }
        for(i=0;i<5;i++)
            for(j=i+1;j<5;j++)
            {
                if(a[i].angle>a[j].angle)
                {
                    node temp=a[i];
                    a[i]=a[j];
                    a[j]=temp;
                }
                else if(a[i].angle==a[j].angle)
                {
                    if(a[i].h>a[j].h)
                    {
                        node temp=a[i];
                        a[i]=a[j];
                        a[j]=temp;
                    }
                }
            }
            printf("%02d:%02d\n",a[2].h,a[2].m);
    }
    return 0;
}


你可能感兴趣的:(HDU 1209 Clock)