HDU1209:Clock(结构体排序)

Clock

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


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
 
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#define N 1000
using namespace std;
struct SS
{
    int hh,mm;
    double r;
    
}f[N];
int cmp(SS a,SS b)
{
    if(a.r!=b.r)
    return a.r<b.r;
    if(a.r==b.r&&a.hh!=b.hh)
    return a.hh<b.hh;
    
}
int main()
{   //freopen("1.txt","r",stdin);
    int i,test,m,n;
    cin>>test;
    while(test--)
    {
        for(i=0;i<5;i++)
        scanf("%d:%d",&f[i].hh,&f[i].mm);
        for(i=0;i<5;i++)
        {
            if(f[i].hh>12)
            {
                f[i].r=fabs(30.0*(f[i].hh-12)+f[i].mm/2.0-6.0*f[i].mm);
                
            }
            else
            {
                f[i].r=fabs(30.0*f[i].hh+f[i].mm/2.0-6.0*f[i].mm);
                
            }
            if(f[i].r>180)
            f[i].r=360-f[i].r;
            
        }
        sort(f,f+5,cmp);
        printf("%02d:%02d\n",f[2].hh,f[2].mm);
        
    }
    return 0;
}

这个题的意思就是输入一个数字然后后面跟着相应的案例,每个案例有五个时间,每个时间按照时针分针的夹角不减序排列,如果夹角相同则按照h来排列,最后求取中间夹角的时间

细节:

时针与分针的夹角的算法:如果时间大于12点那么角度=fabs(30.0*(h-12)+m/2.0-6.0*m)

如果不大于12点的话  r=fabs(30.0*h+m/2.0+m*6.0)

如果r>180

final r=360-r;

然后根据r排列得出一个序列  然后输出中间值   注意%02d也就是说,至少输出两位整数,不满足的话在前面加0  比如说输出05

posted @ 2014-04-12 19:43  盒子先生★  阅读(245)  评论(0编辑  收藏  举报