1006 Sign In and Sign Out (25分)
题目
At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in’s and out’s, you are supposed to find the ones who have unlocked and locked the door on that day.
Input Specification:
Each input file contains one test case. Each case contains the records for one day. The case starts with a positive integer M, which is the total number of records, followed by M lines, each in the format:
ID_number Sign_in_time Sign_out_time
where times are given in the format HH:MM:SS
, and ID_number
is a string with no more than 15 characters.
Output Specification:
For each test case, output in one line the ID numbers of the persons who have unlocked and locked the door on that day. The two ID numbers must be separated by one space.
Note: It is guaranteed that the records are consistent. That is, the sign in time must be earlier than the sign out time for each person, and there are no two persons sign in or out at the same moment.
Sample Input:
3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40
Sample Output:
SC3021234 CS301133
题目大意
每天最早到教室的人需要打开教室的门,每天最晚离开教室的人需要把门上锁。给出当天教室的进出记录,需要找出当天开门和关门的人。
题目分析
为了处理的处理数据的方便性,将所有的进门时间和出门时间都转换成秒来计算,这样在比较时间的先后的顺序的时候会有很大的方便。
在比较数据方面,先选取第一个数据作为初始数据,之后遍历当天所有的记录,不断进行替换,最后找出进门最早和出门的人进心输出。
代码
#include "stdio.h"
int main(){
int M;
int inname,outname;//
int intime,outtime;
char name[1005][20];//输入的名字
int a1[1005],a2[1005],a3[1005];//进门的时刻,a1表示时,a2表示分,a3表示秒
int a[1005];
int b1[1005],b2[1005],b3[1005];//出门的时刻,b1表示时,b2表示分,b3表示秒
int b[1005];
scanf("%d",&M);//输入数据条数
int i;
for (i = 0;i < M;i++){
getchar();
scanf("%s%d:%d:%d %d:%d:%d",name[i],&a1[i],&a2[i],&a3[i],&b1[i],&b2[i],&b3[i]);//输入记录
a[i] = a1[i]*3600 + a2[i]*60 + a3[i];
b[i] = b1[i]*3600 + b2[i]*60 + b3[i];//将进门时间与出门时间转换为秒来使用。
}
intime = a[0];
outtime = b[0];
inname = 0;
outname =0;
for (i = 0;i < M-1;i++){
if (a[i+1] < intime){
intime =a[i+1];
inname = i+1;
}
if (b[i+1] > outtime){
outtime =b[i+1];
outname = i+1;
}
}//找出最早进门和最晚出门的人。
printf("%s %s",name[inname],name[outname]);
return 0;
}