1094.Clock SDNUOJ 1094

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 <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

struct node
{
    char s[10];
    double a, b, angle;
} c[5];

double cal_angle(double h, double m)
{
    double ans;
    ans = m - (h * 5 + m / 12);
    if(ans < 0)
        ans *= -1;
    if(ans > 30 && ans < 60)
        ans = 60 - ans;
    if(ans > 60)
        ans -= 60;
    return ans;
}

int cmp(node a, node b)
{
    if(a.angle != b.angle)
    return a.angle < b.angle;
    else
    {
        if(a.a != b.a)
            return a.a < b.a;
        else
            return a.b < b.b;
    }
}

int main()
{
    int t;
    while(cin >> t)
    {
        while(t--)
        {
            for(int i = 0; i < 5; ++i)
            {
                cin >> c[i].s;
                c[i].a = (c[i].s[0] - '0') * 10 + c[i].s[1] - '0';
                double tem;
                if(c[i].a > 12)
                    tem = c[i].a - 12;
                else
                    tem = c[i].a;
                c[i].b = (c[i].s[3] - '0') * 10 + c[i].s[4] - '0';
                c[i].angle = cal_angle(tem, c[i].b);
            }
            sort(c, c + 5, cmp);
//            for(int i = 0; i < 5; ++i)
//                cout << c[i].s << ' ' << c[i].angle << '\n';
            cout << c[2].s << '\n';
        }
    }
}

全部评论

相关推荐

11-27 17:08
已编辑
牛客_产品运营部_私域运营
腾讯 普通offer 24k~26k * 15,年包在36w~39w左右。
点赞 评论 收藏
分享
猪扒已出闸:方向不够聚焦,看不出来是想找什么方向的工作
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务