Clock——时钟

12 篇文章 0 订阅
7 篇文章 0 订阅

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

题意:

描述:

有一只双指针的模拟时钟:一个小时的指针和一个分针。这双手形成一个角度。角度以双手之间最小的角度来测量。两手之间的角度有大于等于0,小于等于180度的量度。

给定以HH:mm格式书写的五个不同的时间序列,其中HH是表示全时(00)的两位数字(00<=mm<=59)。您将编写一个程序,该程序将查找中值,也就是以关联角度的非递减顺序排序的时间序列的第三个元素。这种关系的破裂方式是在更早的时间之前。

例如,假设给你一个时间序列(06:05,07:10,03:00,21:00,12:55)。因为排序顺序是(12:55,03:00,21:00,06:05,07:10),所以要报告21:00。

输入:

由t测试用例组成。输入文件的第一行给出了测试用例(t)的数目。每个测试用例在一行中给出,其中包含五个不同的次数序列,时间以HH:mm的格式给出,并由一个空格分隔。

输出:

为每个测试用例只打印一行。行将包含给定时间的HH:mm格式中的中值。下面是三个测试用例的示例输入和输出。

思路:

这题主要是求时钟和分钟夹角的大小。然后进行排序。并且还要注意一个小细节问题。当夹角相等时,时间小的放在前面。

夹角求法:分钟旋转一周要60分钟,所以分钟每分钟旋转360度除以60,为6度,而时钟转一周要12小时,一小时等于60分钟,所以分钟转动的速度是时钟转动的12倍。即时钟转动速度为0,5度每分钟。

从而得等公式r=h*30+0.5*m-m*6(h*30:代表时钟准点的度数。而0.5*m:表示转动m分钟时,时钟转动的度数,二者相加即为时钟的总的转的角度。m*6则是分钟转动的角度)

AC码:

#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct node{
	char times[8];
	double mm;//分钟 
	int hh;//小时 
	double angle;//时针和分针的夹角 
}num[8];
bool cmp(node a,node b)
{//将角度从小到大排序。遇到角度相等时,时间靠前的排在前面 
	if(a.angle <b.angle )
	return true;
	else if(a.angle ==b.angle&&(a.hh <b.hh ||(a.hh ==b.hh &&a.mm <b.mm )))
	return true;
	return false;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		for(int i=0;i<5;i++)
		{//5个不同的时间 
			scanf("%s",num[i].times );
			num[i].hh =(num[i].times[0]-'0')*10+(num[i].times[1]-'0');
			num[i].mm =(num[i].times[3]-'0')*10+(num[i].times[4]-'0'); 
			num[i].angle =fabs((double)(num[i].hh%12)*30+(num[i].mm /60)*30-num[i].mm*6);
			if(num[i].angle >180)
			num[i].angle =360-num[i].angle ;
		}
		sort(num,num+5,cmp);
		printf("%s\n",num[2].times );
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值