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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<iostream> 3 #include<string.h> 4 using namespace std; 5 typedef struct rcd{ 6 char id[17]; 7 int hh,mm,ss; 8 }record; 9 int isLater(record a, record b){ 10 if(a.hh > b.hh){ 11 return 1; 12 }else if(a.hh == b.hh && a.mm > b.mm){ 13 return 1; 14 }else if(a.hh == b.hh && a.mm == b.mm && a.ss > b.ss){ 15 return 1; 16 }else{ 17 return 0; 18 } 19 } 20 int main(){ 21 int M; 22 record rd1, rd2, temp1, temp2; 23 rd1.ss = 60; 24 rd1.mm = 60; 25 rd1.hh = 24; 26 rd2.ss = 0; 27 rd2.mm = 0; 28 rd2.hh = 0; 29 scanf("%d", &M); 30 for(int i = 0; i < M; i++){ 31 scanf("%s%d:%d:%d %d:%d:%d", temp1.id, &(temp1.hh), &(temp1.mm), &(temp1.ss), &(temp2.hh), &(temp2.mm), &(temp2.ss)); 32 strcpy(temp2.id, temp1.id); 33 if(isLater(rd1, temp1)){ 34 strcpy(rd1.id, temp1.id); 35 rd1 = temp1; 36 } 37 if(isLater(temp2, rd2)){ 38 strcpy(rd2.id, temp2.id); 39 rd2 = temp2; 40 } 41 } 42 printf("%s %s", rd1.id, rd2.id); 43 cin >> M; 44 return 0; 45 }
总结:
1、本题主要在于选择合适的数据结构存储每一条信息。
2、scanf函数,%s读取字符串时遇到空格换行符结束(会自动加 ),字符数组不用取地址符。
3、struct 结构体可以直接赋值,但成员中有数组时,还需要单独拷贝。