Clock hdu 1209 ——要细心

Clock

  HDU - 1209

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

WA了好几次,因为没有考虑到时针在分针转动时的偏差

WA的代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <queue>
#include <math.h>
#include <algorithm>
using namespace std;
struct node{
	char times[8];
	int hh,mm;
	int angle;
}num[8];

int t0='0';

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<05;i++)
		{
			scanf("%s",num[i].times);
			num[i].hh=(num[i].times[0]-t0)*10+(num[i].times[1]-t0);
			num[i].mm=(num[i].times[3]-t0)*10+(num[i].times[4]-t0);
			num[i].angle=abs((num[i].hh%12)*30-num[i].mm*6);   //WA在了这里
			if(num[i].angle>180)
				num[i].angle=360-num[i].angle;
			printf("%d\n",num[i].angle);
		}
		sort(num,num+5,cmp);
		printf("%s\n",num[2].times);
	}
	return 0;
}

AC:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <queue>
#include <math.h>
#include <algorithm>
using namespace std;
struct node{
	char times[8];
	double mm;
	int hh;
	double angle;
}num[8];

int t0='0';

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<05;i++)
		{
			scanf("%s",num[i].times);
			num[i].hh=(num[i].times[0]-t0)*10+(num[i].times[1]-t0);
			num[i].mm=(num[i].times[3]-t0)*10+(num[i].times[4]-t0);
			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;
			//printf("%llf\n",num[i].angle);
		}
		sort(num,num+5,cmp);
		printf("%s\n",num[2].times);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/LVJINYANJY/article/details/80065231