首页 > ACM题库 > HDU-杭电 > HDU 1209 Clock-排序-[解题报告] C++
2013
12-04

HDU 1209 Clock-排序-[解题报告] C++

Clock

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

02:00
21:00
14:05

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1209

题意:给5个时间(小时:分钟),将之排序。排序规则是按指针锁夹锐角小到大,若夹角相等则按时间早到晚。最后输出中间的那个时间。

mark:题目很简单,角度也很好算:时针每小时走30度,每分钟走0.5度。分针每分钟走6度。做差后取绝对值,再和180比一下,如果大于180,用360减。

为了避免浮点数排序的麻烦,可乘以2倍后排序。3WA。。。排序的时候if语句后面手贱多写了一个;查了半天才查出来。。。

# include <stdio.h>
 # include <stdlib.h>
 
 
 typedef struct TIME{
     int hh, mm ;
     int ang ;
 } TIME ;
 
 
 
 int cmp(const void *a, const void *b)
 {
     TIME *p = (TIME*)a, *q = (TIME*)b ;
     if (p->ang != q->ang)
     return p->ang - q->ang ;
     
     if (p->hh != q->hh) return p->hh - q->hh ;
     return p->mm - q->mm ;
 }
 
 
 int abs(int x){return x<0?-x:x;}
 
 
 int main ()
 {
     int i, T ;
     TIME time[5] ;
     scanf ("%d", &T) ;
     while (T--)
     {
         for(i = 0 ; i < 5 ; i++)
         {
             scanf ("%d:%d", &time[i].hh, &time[i].mm) ;
             time[i].ang = abs((time[i].hh%12)*60+time[i].mm - time[i].mm*12) ;
             if (time[i].ang > 360) time[i].ang = 720 - time[i].ang ;
         }
         qsort(time, 5, sizeof(time[0]), cmp) ;
         printf ("%02d:%02d\n", time[2].hh, time[2].mm) ;
     }
     return 0 ;
 }