1094.Clock
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.
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
#include<iostream>
#include<cstdio>#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
struct mytime
{
int shi;
int fen;
double du;
int time;
}t[5];
bool cmp(mytime bb,mytime cc)
{
if(bb.du != cc.du) return bb.du < cc.du;
return bb.time < cc.time;
// if(b.du < c.du) return true;
// else if(b.du > c.du) return false;
// else
// {
// if(b.shi < c.shi) return true;
// else if(b.shi > c.shi) return false;
// else
// {
// if(b.fen < c.fen) return true;
// else if(b.fen > c.fen) return false;
// }
//
// }
}
int main()
{
int a,b,i,q;
double cop;
cin>>q;
while(q--)
{
for(i = 0;i < 5;i++)
{
scanf("%d:%d",&a,&b);
t[i].shi = a;
t[i].fen = b;
t[i].time = a * 60 + b;
if(a > 12)
{
a = a - 12;
}
cop = fabs(a * 30 - b * 6 + b * 0.5);
if(cop > 180) cop = 360 - cop;
t[i].du = cop;
}
sort(t,t+5,cmp);
printf("%02d:%02d\n",t[2].shi,t[2].fen);
}
return 0;
}
WA了好几遍,现在终于明悟:是我考虑的太少了,分针的指向对时针的指向是有所影响的,一分钟会让时针多偏移0.5度,依次类推,而我没有考虑到时针的具体指向,定义了int存角度,没有算是min*0.5的角度
以后看来还得联系联系实际,不能做死题啊~~