HDU 1209 Clock(简单水题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1209

Clock

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


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
 
Recommend
mcqsmall
 
简单的时钟角度计算问题,1个小时对应30度,1分钟对应1/60*30=0.5度,计算下时针和分针之间的角度差,这里需要判断是否大于12点,大于12点需减去12,角度差需取绝对值,最后判断下是否超过180度就可以,结构体排序就不说了。。。
 
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cmath>
 5 using namespace std;
 6 struct node
 7 {
 8     int hh;
 9     int mm;
10     double rad;
11 } time[7];
12 bool cmp(node a, node b)
13 {
14     if(a.rad != b.rad)return a.rad < b.rad;
15     else if(fabs(a.rad - b.rad) < 1e-9 && a.hh != b.hh)return a.hh < b.hh;
16     else return a.mm < b.mm;
17 }
18 double calrad(int hh, int mm)
19 {
20     double temp = fabs((hh >= 12 ? hh - 12 : hh) * 30 * 1.0 + mm / 2.0 - mm * 6.0);
21     return (temp > 180 ? 360 - temp : temp);
22 }
23 void getdata()
24 {
25     int i;
26     for(i = 0; i < 5; i++)
27     {
28         scanf("%d:%d", &time[i].hh, &time[i].mm);
29         time[i].rad = calrad(time[i].hh, time[i].mm);
30     }
31     sort(time, time + 5, cmp);
32 }
33 int main()
34 {
35     int t;
36     scanf("%d", &t);
37     while(t--)
38     {
39         getdata();
40         printf("%02d:%02d\n", time[2].hh, time[2].mm);
41     }
42     return 0;
43 }
 
原文地址:https://www.cnblogs.com/lfeng/p/3052703.html