浙江大学计算机系硕士研究生复试题目解答(2)

本文介绍了一种通过分析机房签到与签离记录来确定每天第一个开门和最后一个关门人员的方法。该方法使用C语言实现,能够处理多天的数据记录,并准确找到目标人员。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

谁是开门关门的人?(10分)
题目要求:
每天第一个到机房的人要把门打开,最后一个离开的人要把门关好。现有一堆杂乱的机房签
到、签离记录,请根据记录找出当天开门和关门的人。
具体的输入输出格式规定如下:
输入格式:测试输入的第一行给出记录的总天数N ( > 0 )。下面列出了N天的记录。
每天的记录在第一行给出记录的条目数M ( > 0 ),下面是M行,每行的格式为


证件号码签到时间签离时间

其中时间按“小时:分钟:秒钟”(各占2位)给出,证件号码是长度不超过15的字符串。
输出格式:对每一天的记录输出1行,即当天开门和关门人的证件号码,中间用1空格分隔。
注意:在裁判的标准测试输入中,所有记录保证完整,每个人的签到时间在签离时间之前,
且没有多人同时签到或者签离的情况。

要求完整的输入输出如下:

输入:

结果:

源代码如下:

#include "stdio.h"
#include "string.h"
#include "malloc.h"
#define idlength 15
struct Person
{
char id[idlength+1];
char Log[10];
char Leave[10];
};
struct Day
{
Person *work;
int number;
};
struct Day *CreateDayLog(int);
struct Person *CreatePersonLog(int);
int Process(struct Person *,char *);
struct Person *Earliest(struct Day*);
struct Person *Latiest(struct Day*);

int main()
{
int i,j,k;
Day *process;
Person *p;
printf("/nPlease Enter The Days:");
scanf("%d",&i);
process=CreateDayLog(i);
for(j=0;j<i;j++)
{
printf("/nIn %dth day:/n",j+1);
if((process+j)->number!=0)
{
p=Earliest(process+j);
printf("The earliest user and his info is:/n");
printf("%s,%s,%s",p->id,p->Log,p->Leave);
printf("/n");
p=Latiest(process+j);
printf("The latiest user and his info is:/n");
printf("%s,%s,%s",p->id,p->Log,p->Leave);
}
else
printf("There is no user that day!/n");
}
return 1;
}
struct Day *CreateDayLog(int i)
{
int j,k;
struct Day *day=(struct Day *)malloc(i*sizeof(struct Day));
for(j=0;j<i;j++)
{
printf("/nThe number of users of the %dth day",j+1);
scanf("%d",&k);
(day+j)->number=k;
(day+j)->work=CreatePersonLog(k);
}
return day;
}
struct Person *CreatePersonLog(int i)
{
int j;
Person *user=(struct Person*)malloc(sizeof(struct Person)*i);
char detail[idlength+20],*t;
gets(detail);
for(j=0;j<i;j++)
{
printf("/nThe deatails of the %dth person/n",j+1);
t=gets(detail);
Process(user+j,t);
}
return user;
}
int Process(struct Person *p,char *c)
{
int i=0,j=0,k=0,status=0;
char time[8];
for(;*c!='/0';c++)
{
if(*c==' ')
{
if(status==0)
{
p->id[i]='/0';
status++;
continue;
}
if(status==1)
status++;
}
if(*c!=' ')
{
if(status==0)  /* Now meets the id */
{
p->id[i]=*c;
i++;
}
else
if(status==1)  /*Now meets the log time */
{
p->Log[j]=*c;
j++;
}
else
if(status==2)  /*Now meets the leave time */
{
p->Leave[k]=*c;
k++;
}
}
}
p->id[i]=p->Log[j]=p->Leave[k]='/0';
}
struct Person *Earliest(struct Day* day)
{
int minhour=25,minminute=61,minsecond=61;
int i,early,temphour,tempminu,tempsec;
struct Person *p=day->work,*result;
result=p;
for(i=0;i<day->number;i++,p++)
{
temphour=(p->Log[0]-'0')*10+(p->Log[1]-'0');
tempminu=(p->Log[3]-'0')*10+(p->Log[4]-'0');
tempsec=(p->Log[6]-'0')*10+(p->Log[7]-'0');
if(temphour<minhour)
{
minhour=temphour;
minminute=tempminu;
minsecond=tempsec;
early=i;
}
else if(temphour==minhour&&tempminu<minminute)
{
minminute=tempminu;
minsecond=tempsec;
early=i;
}
else if(temphour==minhour&&tempminu==minminute&&tempsec<minsecond)
{
minsecond=tempsec;
early=i;
}
}
return result+early;
}
struct Person *Latiest(struct Day* day)
{
int maxhour=-23,maxminute=-59,maxsecond=-59;
int i,late,temphour,tempminu,tempsec;
struct Person *p=day->work,*result;
result=p;
for(i=0;i<day->number;i++,p++)
{
temphour=(p->Leave[0]-'2')*10+(p->Leave[1]-'3');
tempminu=(p->Leave[3]-'5')*10+(p->Leave[4]-'9');
tempsec=(p->Leave[6]-'5')*10+(p->Leave[7]-'9');
if(temphour>maxhour)
{
maxhour=temphour;
maxminute=tempminu;
maxsecond=tempsec;
late=i;
}
else if(temphour==maxhour&&tempminu>maxminute)
{
maxminute=tempminu;
maxsecond=tempsec;
late=i;
}
else if(temphour==maxhour&&tempminu==maxminute&&tempsec>maxsecond)
{
maxsecond=tempsec;
late=i;
}
}
return result+late;
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值