码迷,mamicode.com
首页 > 其他好文 > 详细

杭电 HDU 1209 Clock

时间:2015-04-01 09:37:55      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:acm   c++   杭电   算法   编程   

Clock

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5140    Accepted Submission(s): 1589


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
 

Source

Asia 2003(Seoul)

这个题 刚开始没思路,试想小时是从1---23两个指针都在变化,角度如何确定?看了看网上竟然没有多少相关资料!干脆自己想,试想可以借助12点,把小时和分钟和12点的夹角

之差即为两个表针的夹角,这样很容易表示结构体中的angle,但是有一个问题,如果小时大于12呢?这时候确定度数就要对12取余,这个过程中不能变动结构体中的数据成员,

否则影响angle相同下的hour排序。注意度数是实型,故wa一次!

Code:

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;

struct clock
{
	int hour;
	int minute;
    double angle;//
}time[5];

bool cmp(clock V,clock X)//结构体多级排序
{
	if(V.angle!=X.angle)
		return V.angle<X.angle;
	else if((V.angle==X.angle)&&(V.hour==X.hour))
       return V.minute<X.minute;
	else 
		return V.hour<X.hour;
}
int main()
{
	char ch;int T;
	cin>>T;
	
	while(T--)
	{
		for(int j=0;j<5;j++)
		{
			  cin>>time[j].hour>>ch>>time[j].minute;
		
			 double anglehour=( time[j].hour%12+time[j].minute*1.0/60)*30;
			  double anglemin=time[j].minute*6;
			  time[j].angle=abs(anglehour-anglemin)>180?(360-abs(anglehour-anglemin)):abs(anglehour-anglemin);
			}
			sort(time,time+5,cmp);
			printf("%02d:%02d\n",time[2].hour,time[2].minute);
	}
	return 0;
}


 

 

杭电 HDU 1209 Clock

标签:acm   c++   杭电   算法   编程   

原文地址:http://blog.csdn.net/lsgqjh/article/details/44787691

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!