Clock——時鐘

韓小妹發表於2018-08-10

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;
}

 

相關文章