题目如下:
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:40Sample Output:
SC3021234 CS301133题目要求从一些记录中找出最早进入的和最晚离开的,只针对此目的考虑,不必存储这些数据,而应该动态的记录最小值和最大值,最后输出,实现思路是对每一条记录进行检查,如果发现新记录的Sign In时间早于目前储存的Sign In最早时间,则更新minID和最早Sign In时间,对于Sigh Out时间同理,这样最后得到的minID和maxID即为所求。
题目中的细节在于对字符的存储和处理,首先建立char数组来缓存新输入的ID、两个时间,注意char数组的规模应该≥字符个数+1,同时建立两个与前面同规模的char数组存储最早Sign In的minID和最晚Sign Out的maxID,另外建立一个结构体存储时间解析后的时、分、秒数值。
注意事项:
1.在将新输入的ID(设此变量为newID)赋值给minID和maxID时,使用strcpy函数而不要直接用=赋值!
2.对于三个字符串以空格为分隔符的输入,采用scanf("%s %s %s",...),用%s之间的字符来决定分隔符。
3.使用VS等高级编译器可以在不包含头文件的情况下使用函数,例如本题中的strcpy,记得要加上string.h,否则会编译错误。
AC代码如下:
#include<iostream> #include<string.h> using namespace std; typedef struct Time_s * Time; struct Time_s{ int hour; int minute; int second; }; int charToNum(char c){ return c - '0'; } void setTimeWithBuffer(char s[9], Time t){ t->hour = charToNum(s[0]) * 10 + charToNum(s[1]); t->minute = charToNum(s[3]) * 10 + charToNum(s[4]); t->second = charToNum(s[6]) * 10 + charToNum(s[7]); } int main(){ char newID[16] = { 0 }; char minID[16] = { 0 }; char maxID[16] = { 0 }; Time_s minTime, maxTime, newTime; minTime.hour = 25; maxTime.hour = -1; int N; char inTime[9] = { 0 }, outTime[9] = { 0 }; cin >> N; bool setKey = false; for (int i = 0; i < N; i++){ scanf("%s %s %s", newID, inTime, outTime); setTimeWithBuffer(inTime, &newTime); if (newTime.hour < minTime.hour){ setKey = true; } else if (newTime.hour == minTime.hour){ if (newTime.minute < minTime.minute){ setKey = true; } else if (newTime.minute == minTime.minute){ if (newTime.second < minTime.second){ setKey = true; } } } if (setKey){ setKey = false; strcpy(minID, newID); minTime.hour = newTime.hour; minTime.minute = newTime.minute; minTime.second = newTime.second; } setTimeWithBuffer(outTime, &newTime); if (newTime.hour > maxTime.hour){ setKey = true; } else if (newTime.hour == maxTime.hour){ if (newTime.minute > maxTime.minute){ setKey = true; } else if (newTime.minute == maxTime.minute){ if (newTime.second > maxTime.second){ setKey = true; } } } if (setKey){ setKey = false; strcpy(maxID, newID); maxTime.hour = newTime.hour; maxTime.minute = newTime.minute; maxTime.second = newTime.second; } } printf("%s %s ", minID, maxID); return 0; }