HUT 排序训练赛 G题

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
#include
#include
#include
struct clock
{
double d;
int s; 
int h;
int m;
}cl[5];
int cmp(const void *b,const void *c)
{
struct clock *p=(struct clock *)b;
struct clock *q=(struct clock *)c;
if(p->d==q->d)
return p->ss;    //特别要注意p->ss跟p->s-q->s的区别,用于没学过,参考网上的对结构体排序,没弄懂,所以刚开是用了p->s-q->s,后来幸好学长提醒了。
else
return p->dd; 
}
main()
{
int n,i,j,w;
char a[10];
scanf("%d",&n);
for(j=0;j
{
for(i=0;i<5;i++)
{
scanf("%s",a);
 sscanf(a,"%d:%d",&cl[i].h,&cl[i].m);
 if(cl[i].h>=12)
 w=cl[i].h-12;
 else
 w=cl[i].h;
 cl[i].s=cl[i].h*60+cl[i].m;
     cl[i].d=fabs((double)30*w+(double)cl[i].m*1.0/2-(double)6*cl[i].m);  //刚开始求角度的公式错了,找了大半天都没找出来,后来还请了同学和组长帮忙看,也看好久,好吧,最后证实了确实是公式错误,要吸取教训,有时候虽然结果是对的,但隐藏的某些数据算错了,或者说只使用与一般情况,对于普遍的数据使用不了。这次就是没有考虑到类似12:56的数据,只是考虑类似12:55的。。
 if(cl[i].d>180)
 cl[i].d=360-cl[i].d;
}
qsort(cl,5,sizeof(cl[0]),cmp);
printf("d:d\n",cl[2].h,cl[2].m);

}
}

转载于:https://www.cnblogs.com/tanjianwen/p/5245447.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值