1006 Sign In and Sign Out【PAT (Advanced Level) Practice】

1006 Sign In and Sign Out【PAT (Advanced Level) Practice】

原题链接:预览题目详情 - 1006 Sign In and Sign Out (pintia.cn)

1.题目原文

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 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 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

2. 题目翻译

在每一天的开始,第一个在计算机房签到的人将解锁门,最后一个签退的人将锁上门。根据签到和签退的记录,你需要找到在当天解锁和锁上门的人员。

输入规范:

每个输入文件包含一个测试用例。每个案例包含一天的记录。案例以正整数 M M M 开始,表示记录总数,然后是 M M M 行,每行的格式如下:

ID号 签到时间 签退时间

时间以HH:MM:SS的格式给出,而ID号是一个不超过15个字符的字符串。

输出规范:

对于每个测试用例,在一行中输出当天解锁和锁门的人员的ID号。这两个ID号之间必须用一个空格隔开。

注意:保证记录是一致的。也就是说,每个人的签到时间必须早于签退时间,并且没有两个人在同一时刻签到或签退。

示例输入:

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

示例输出:

SC3021234 CS301133

3.解题思路

3.1题目分析

根据每天的签到和签退记录找到解锁和锁门的人员,即时间最早和时间最晚的人。

输入每个人的ID号、签到时间和签退时间,输出当天解锁和锁门的人员的ID号。

每个人的签到时间早于签退时间,且没有两个人在同一时刻签到或签退。

3.2基本思路

通过迭代遍历所有记录,比较签到和签退时间,找到当天解锁和锁门的人员。

3.3详解步骤

  1. 初始化开门和关门记录: 将第一条记录同时设定为开门和关门的初始记录。
  2. 循环比较: 从第二条记录开始,通过迭代遍历剩余的记录。在每次迭代中,比较当前记录的签到时间和签退时间与当前开门和关门记录的时间,更新开门和关门记录。通过维护两组变量(开门记录和关门记录)来实现对签到和签退时间的比较,并不断更新这两组变量,最终得到当天解锁和锁门的人员的ID号。
  3. 输出结果: 循环结束后,输出开门和关门记录的ID号。

3.4注意要点

算法的关键在于使用字符串比较函数 strcmp 来比较时间。

4.参考答案

#include <stdio.h>
#include <string.h>

int main() {
    
    
    char id[16], in[10], out[10];
    char open_id[16], close_id[16], open_in[10], close_out[10];
    int m, j;

    // 输入记录总数
    scanf("%d", &m);

    // 读取第一条记录
    scanf("%s %s %s", id, in, out);

    // 初始化开门和关门的记录为第一条记录
    strcpy(open_id, id);
    strcpy(close_id, id);
    strcpy(open_in, in);
    strcpy(close_out, out);

    // 循环读取剩余记录
    for (j = 1; j < m; j++) {
    
    
        scanf("%s %s %s", id, in, out);

        // 比较更新开门记录
        if (strcmp(open_in, in) > 0) {
    
    
            strcpy(open_in, in);
            strcpy(open_id, id);
        }

        // 比较更新关门记录
        if (strcmp(close_out, out) < 0) {
    
    
            strcpy(close_out, out);
            strcpy(close_id, id);
        }
    }

    // 输出开门和关门的人员ID号
    printf("%s %s\n", open_id, close_id);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40171190/article/details/134746985