这个题有一个很坑的地方,就是日程表的顺序不一定是按时间排序,需要自己排序
比较简单的模拟题,还是练代码能力
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef struct
{
int h;
int m;
} t;
t st[105],ed[105],k;
typedef struct
{
int sh;
int sm;
int eh;
int em;
} o;
o a[105];
int cmp(const void *_x,const void *_y)
{
o *x=(o *)_x;
o *y=(o *)_y;
if(x->sh==y->sh)
return x->sm-y->sm;
return x->sh-y->sh;
}
int main()
{
int u,p=0;
char ss[300];
while(scanf("%d",&u)==1)
{
p++;
st[0].h=10;
st[0].m=0;
ed[u].h=18;
ed[u].m=0;
for(int i=0; i<u; i++)
{
scanf("%d:%d %d:%d",&a[i].sh,&a[i].sm,&a[i].eh,&a[i].em);
gets(ss);
}
qsort(a,u,sizeof(a[0]),cmp);
for(int i=0; i<u; i++)
{
ed[i].h=a[i].sh;
ed[i].m=a[i].sm;
st[i+1].h=a[i].eh;
st[i+1].m=a[i].em;
}
int max=0;
for(int i=0; i<=u; i++)
{
int e=(ed[i].h-st[i].h)*60+ed[i].m-st[i].m;
if(e>max)
{
max=e;
k.h=st[i].h;
k.m=st[i].m;
}
}
if(max<60)
printf("Day #%d: the longest nap starts at %.2d:%.2d and will last for %d minutes.\n",
p,k.h,k.m,max);
else
printf("Day #%d: the longest nap starts at %.2d:%.2d and will last for %d hours and %d minutes.\n",
p,k.h,k.m,max/60,max%60);
}
return 0;
}