题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1209
大意是,给你5个时间,都是mm::nn格式的,按照时针与分针之间的夹角排序,如果相等则取时间小的,输出中间的那个时间。
挺简单的一道水题,刚好熟悉一下才学的strtok函数。
唯一需要注意的是在输出的时候由于strtok函数已经修改了原始的字符串,所以需要重新格式化输出一下:
代码如下:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
char str[6];
struct clock
{
int h,m;
int time;//总时间
double du;//角度
}data[5];
bool cmp(clock a,clock b)
{
if(a.du==b.du)
return a.time<b.time;
else
return a.du<b.du;
}
double angle(int h,int m)
{
if(h>=12) h=h-12;
double deg_h=h*30+m*0.5;//时针角度
double deg_m=m*6;//分针角度
double ans=abs(deg_h-deg_m);//差值即为夹角
if(ans>=0&&ans<=180);
else
ans=360-ans;
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
for(int i=0;i<5;i++)
{
scanf("%s",&str);
char *p;
p=strtok(str,":");
sscanf(p,"%d",&data[i].h);
p=strtok(NULL,":");
sscanf(p,"%d",&data[i].m);
data[i].time=data[i].h*60+data[i].m;
data[i].du=angle(data[i].h,data[i].m);
}
sort(data,data+5,cmp);
printf("%02d:%02d\n",data[2].h,data[2].m);//输出需要另外考虑下。
}
return 0;
}