前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ZOJ 2680 Clock()数学

ZOJ 2680 Clock()数学

作者头像
全栈程序员站长
发布2022-07-06 19:36:10
3820
发布2022-07-06 19:36:10
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是全栈君

主题链接: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

代码语言:javascript
复制
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

代码语言:javascript
复制
02:00
21:00
14:05
----

Source: Asia 2003, Seoul (South Korea)

题意:

//给出 5 个时刻,按时钟的时针,分针夹角从小到大排序, //输出中间的时刻。

代码例如以下:

代码语言:javascript
复制
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
struct TIME
{
    int h;
    int m;
    int angle;
}a[7];

int cal(TIME TT)
{
    if(TT.h > 12)
    {
        TT.h-=12;
    }
    int tt = abs((TT.h*60 + TT.m) - TT.m*12);
    //原式为:TT.h*30+(TT.m/60)*30-a.m*6;
    if(tt > 360)
        tt = 720 - tt;
    return tt;
}
bool cmp(TIME A, TIME B)
{
    if(A.angle != B.angle)
    {
        return A.angle < B.angle;
    }
    else if(A.h != B.h)
    {
        return A.h < B.h;
    }
    else
        return A.m < B.m;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        for(int i = 0; i < 5; i++)
        {
            scanf("%d:%d",&a[i].h,&a[i].m);
            a[i].angle = cal(a[i]);
        }
        sort(a,a+5,cmp);
        printf("%02d:%02d\n",a[2].h,a[2].m);
    }
    return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116756.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年1月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档