C

来源:互联网 发布:巫师3帧数优化 编辑:程序博客网 时间:2024/05/16 12:54

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

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespacestd;
typedef struct
{
int h,m;
double deg;
}S;
bool cmp(S a,S b){
if(a.deg < b.deg) return1;
if(a.deg == b.deg && a.h < b.h) return 1;
if(a.deg == b.deg && a.h == b.h && a.m < b.m) return 1;//当角度一样大时,时间小的排在前面
return 0;

}
int main(){
S a[5];int T,i;
//freopen("1.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
for(i=0;i<5;i++){
scanf("%d:%d",&a[i].h,&a[i].m);
a[i].deg=(double)fabs(((a[i].h%12)+a[i].m/60.0)*30-a[i].m*6);//*fabs之前没指定他的返回值的类型,结果一直无法ac

     if(a[i].deg>180)a[i].deg=360-a[i].deg;     //fabs不能用abs替代因为fabs可以对精度大的数值取绝对值,而abs只对整数作用
}
sort(a,a+5,cmp);
printf("%02d:%02d\n",a[2].h,a[2].m);//这种输出格式也挺有意思
}
return 0;


}

原创粉丝点击