HUT 排序训练赛 G - Clock

Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

[]   [Go Back]   [Status]  

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)

 

【题目来源】

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=41455#problem/G

【题目大意】

输入5个时间点,要求计算时针与分针的夹角,然后比较夹角大小,输出第三大的那个时间点。

【细节】

注意这道题不需要考虑去重,也就是说直接比较输出就可。

还有就是角度相同的时候要比较时针,时针相同时比较分针。

角度最好定义为double型。

当时间大于或等于12时,与12相减然后取绝对值。

【解题思路】首先还是定义一个结构体用来存储输入的数据,然后就是输入,接着计算角度,结构体排序,输出。

【角度计算方法】

大家已经认识了钟表。钟表上的分针、时针在不停息地转动着,两针有时相互重合,有时相互垂直,有时又成一条直线,而求时针、分针形成的各种不同位置所需的时间,就构成了饶有兴趣的时钟问题。

 [基础知识]

(1)周角是360°,钟面上有12个大格,每个大格是360°÷12=30°;有60个小格,每个小格是360°÷60=6°。

(2)时针每小时走一个大格(30°),所以时针每分钟走30°÷60=0.5°;分针每小时走60个小格,所以分针每分钟走6°.

一般来说,已知钟面的时刻求时针和分针所成夹角的度数(小于或等于180°的角度),可以找出时针(整时刻)和分针(当前时刻)之间相差的大格或小格数。求出相应度数以后,再减去时针所走的度数(用分针数乘以0.5°)

 

下面是我的AC代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>

#define eps 1e-6

using namespace std;

struct clock
{
    char st[6];
    int h,m;
    int time;
    double du;
}c[5];

bool cmp(clock a,clock b)
{
    if(a.du<b.du)
      return true;
     if(a.du>b.du)
        return false;
     if(a.du==b.du)
     {
         return a.time>b.time? false:true;
     }
}

double cnt(int h,int m)
{
    if(h>=12) h=h-12;
    double dh=h*30+m*0.5;
    double dm=m*6;

    double ma,mi;
    ma=max(dh,dm);
    mi=min(dh,dm);

    double ans=ma-mi;
    if(ans>=0&&ans<=180);
    else
        ans=360-ma+mi;
    return ans;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        for(int i=0;i<5;i++)
        {
            cin>>c[i].st;
            c[i].h=(c[i].st[0]-'0')*10+(c[i].st[1]-'0');
            c[i].m=(c[i].st[3]-'0')*10+(c[i].st[4]-'0');
            c[i].time=c[i].h*60+c[i].m;
            c[i].du=cnt(c[i].h,c[i].m);
        }

        sort(c,c+5,cmp);
        cout<<c[2].st<<endl;
    }

    return 0;
}

 

你可能感兴趣的:(Lock)