HDOJ 1209 Clock

排序函数很坑爹。

Clock

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


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
300:00 01:00 02:00 03:00 04:0006:05 07:10 03:00 21:00 12:5511:05 12:05 13:05 14:05 15:05
 
Sample Output
02:0021:0014:05
 
Source
 
Recommend
mcqsmall
 
 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cmath>
 5  
 6 #define eps 1e-6
 7  
 8 using namespace std;
 9  
10 struct clock
11 {
12     char st[6];
13     int h,m;
14     int time;
15     double du;
16 }c[5];
17  
18 bool cmp(clock a,clock b)
19 {
20     if(a.du<b.du)
21       return true;
22      if(a.du>b.du)
23         return false;
24      if(a.du==b.du)
25      {
26          return a.time>b.time? false:true;
27      }
28 }
29  
30 double cnt(int h,int m)
31 {
32     if(h>=12) h=h-12;
33     double dh=h*30+m*0.5;
34     double dm=m*6;
35  
36     double ma,mi;
37     ma=max(dh,dm);
38     mi=min(dh,dm);
39  
40     double ans=ma-mi;
41     if(ans>=0&&ans<=180);
42     else
43         ans=360-ma+mi;
44     return ans;
45 }
46  
47 int main()
48 {
49     int t;
50     cin>>t;
51     while(t--)
52     {
53         for(int i=0;i<5;i++)
54         {
55             cin>>c[i].st;
56             c[i].h=(c[i].st[0]-'0')*10+(c[i].st[1]-'0');
57             c[i].m=(c[i].st[3]-'0')*10+(c[i].st[4]-'0');
58             c[i].time=c[i].h*60+c[i].m;
59             c[i].du=cnt(c[i].h,c[i].m);
60         }
61  
62         sort(c,c+5,cmp);
63 /*
64         for(int i=0;i<5;i++)
65             cout<<c[i].h<<":"<<c[i].m<<",,,,"<<c[i].time<<"..."<<c[i].du<<endl;
66 */
67         cout<<c[2].st<<endl;
68     }
69  
70     return 0;
71 }
原文地址:https://www.cnblogs.com/CKboss/p/3095380.html