PAT甲级 1006 Sign In and Sign Out (25 分) 字符串比较、拷贝

1006 Sign In and Sign Out (25 分)
Time limit: 400 ms
Memory limit: 64 MB
Source limit: 16 KB

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

Each input file contains one test case. Each case contains the records for one day. The case starts with a positive integer M M M, which is the total number of records, followed by M M 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

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.

Example

input
3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40
output
SC3021234 CS301133

题意解析:

输入 N N N个人,输出最早到达与最晚离开的人的名字。

思路:

一个简单的想法是把时间的字符串转换为int变量处理。不过这里可以直接通过比较两个时间的字符串的字典序来判断两个时间的早晚。在C字符串中使用strcmp()来比较两个字符串的字典序,用strcpy()来拷贝字符串。

为了方便,先读入一个人,那么此时这个人既是最早来的也是最晚离开的。因此earlyNamelastName都为这个人的名字。紧接着读完剩余的 N − 1 N-1 N1个人并比较它们的到达时间与离开时间。

参考代码:

#include 
#include 
#define MAXN 20
int N;
char earlyName[MAXN], lastName[MAXN], earlyTime[MAXN], lastTime[MAXN], name[MAXN], s1[MAXN], s2[MAXN];
int main() {
    scanf("%d%s%s%s", &N, earlyName, earlyTime, lastTime);
    strcpy(lastName, earlyName);
    while (--N) {
        scanf("%s%s%s", name, s1, s2);
        if (strcmp(earlyTime, s1) > 0) {
            strcpy(earlyTime, s1);
            strcpy(earlyName, name);
        }
        if (strcmp(lastTime, s2) < 0) {
            strcpy(lastTime, s2);
            strcpy(lastName, name);
        }
    }
    printf("%s %s\n", earlyName, lastName);
    return 0;
}

你可能感兴趣的:(PAT甲级)