Description
There is an analog clock with two hands: anhour hand and a minute hand. The two hands form an angle. The angle is measuredas the smallest angle between the two hands. The angle between the two handshas a measure that is greater than or equal to 0 and less than or equal to 180degrees.
Given a sequence of five distinct timeswritten 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, thethird element of the sorted sequence of times in a nondecreasing order of theirassociated angles. Ties are broken in such a way that an earlier time precedesa 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. Thenumber of test cases (T) is given on the first line of the input file. Eachtest case is given on a single line, which contains a sequence of five distincttimes, where times are given in the format hh : mm and are separated by asingle 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. Thefollowing 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
这个题的意思是说:把每个时间在钟表上对应的时针和分针之间的夹角(小于180°)按顺序排列,把中间的一个输出。
这是WA的代码。。。
#include <iostream>
#include <cstdio>
#include <memory.h>
#include <cmath>
#include <algorithm>
using namespace std;
struct stuff{
int ho;
int mi;
int sign;
}time[10];
bool cmp(stuff a, stuff b)
{
if(a.sign > b.sign)
return true;
else if(a.sign == b.sign)
{
if(a.ho > b.ho)
return true;
else
return false;
}
else
return false;
}
int main()
{
int n;
scanf("%d", &n);
while(n--)
{
memset(time, 0, sizeof(time));
int i;
for(i = 0; i < 5; i++)
{
scanf("%d:%d", &time[i].ho, &time[i].mi);
if(time[i].ho > 12)
time[i].sign = abs(30.0 * (time[i].ho - 12) - time[i].mi * 6.0 );
else
time[i].sign = abs(30.0 * time[i].ho - time[i].mi * 6.0);
if(time[i].sign > 180)
time[i].sign = 360 - time[i].sign;
}
sort(time, time + 5, cmp);
printf("%02d:%02d\n", time[2].ho, time[2].mi);
}
return 0;
}
为什么WA呢?因为当分针从12开始走动时,时针也在慢慢的动(生活常识),所以正确的应该是:
#include <iostream>
#include <cstdio>
#include <memory.h>
#include <cmath>
#include <algorithm>
using namespace std;
struct stuff{
int ho;
int mi;
int sign;
}time[10];
bool cmp(stuff a, stuff b)
{
if(a.sign > b.sign)
return true;
else if(a.sign == b.sign)
{
if(a.ho > b.ho)
return true;
else
return false;
}
else
return false;
}
int main()
{
int n;
scanf("%d", &n);
while(n--)
{
memset(time, 0, sizeof(time));
int i;
for(i = 0; i < 5; i++)
{
scanf("%d:%d", &time[i].ho, &time[i].mi);
if(time[i].ho > 12)
time[i].sign = abs(30.0 * (time[i].ho - 12) - time[i].mi * 6.0 + time[i].mi /2.0);
else
time[i].sign = abs(30.0 * time[i].ho - time[i].mi * 6.0 + time[i].mi /2.0);
if(time[i].sign > 180)
time[i].sign = 360 - time[i].sign;
}
sort(time, time + 5, cmp);
printf("%02d:%02d\n", time[2].ho, time[2].mi);
}
return 0;
}
你能看出改动在哪吗?