Clock(排序)

这篇博客介绍了如何编写一个程序,通过解析给定的五个不同时刻,计算它们对应时钟角度的中位数,角度由时针与分针之间的最小夹角决定。算法涉及时间转换、角度计算和非递减排序。示例用C++代码展示了如何找到给定时间序列(如06:05, 07:10, 03:00, 21:00, 12:55)的中位时间21:00。
摘要由CSDN通过智能技术生成
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

AC代码

#include <iostream>
#include <algorithm>
using namespace std;
//遇到格式化输入输出最好还是使用C语言
//angle = abs(60h+0.5m-6m)即时针与分针的角度之差
const int N = 5;
struct time{
	int h, m;
	double angle;
}a[N];

bool cmp(time x, time y)
{
	if(x.angle == y.angle) return x.h < y.h;
	return x.angle < y.angle;
}

int main()
{
	int n, temp;
	scanf("%d", &n);
	while(n --)
	{
		for(int i = 0; i < 5; i ++)
		{
			scanf("%d:%d", &a[i].h, &a[i].m);
			if(a[i].h >= 12) temp = a[i].h - 12;
			else temp = a[i].h;
			a[i].angle = abs(30.0 * temp + 0.5 * a[i].m - 6.0 * a[i].m);
			a[i].angle = min(a[i].angle, 360-a[i].angle);
		}
		sort(a, a+N, cmp);
		printf("%02d:%02d\n", a[2].h, a[2].m);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值