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

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

Original question link:Preview question details - 1006 Sign In and Sign Out (pintia.cn)

1.Original text of the title

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. Title translation

At the beginning of each day, the first person to sign in to the computer room will unlock the door, and the last person to sign out will lock the door. Based on the check-in and check-out records, you need to find the person who unlocked and locked the door that day.

Input specification:

Each input file contains one test case. Each case contains one day's worth of records. Case in positive integer M M M starts with the total number of records, followed by M M M lines, the format of each line is as follows:

ID号 签到时间 签退时间

The time is given in the format HH:MM:SS and ID号 is a string of no more than 15 characters.

Output specifications:

For each test case, output in one row the ID number of the person who unlocked and locked the door that day. The two ID numbers must be separated by a space.

NOTE: Ensure records are consistent. That is to say, everyone's check-in time must be earlier than the check-out time, and no two people will sign in or out at the same time.

Example input:

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

Example output:

SC3021234 CS301133

3. Problem-solving ideas

3.1 Question analysis

Find the person who unlocks and locks the door based on the daily check-in and check-out records, that is, the earliest and latest person.

Enter each person's ID number, check-in time and check-out time, and output the ID number of the person who unlocked and locked the door that day.

Everyone's check-in time is earlier than the check-out time, and no two people sign in or out at the same time.

3.2 Basic ideas

By iteratively traversing all records, comparing check-in and check-out times, find the person who unlocked and locked the door that day.

3.3 Detailed steps

  1. Initialize the opening and closing records: Set the first record as the initial record of opening and closing at the same time.
  2. Loop comparison: Starting from the second record, iterate through the remaining records. In each iteration, compare the currently recorded check-in time and check-out time with the current door opening and closing record times, and update the door opening and closing records. By maintaining two sets of variables (opening records and closing records), the check-in and check-out times are compared, and these two sets of variables are continuously updated, and finally the ID numbers of the people who unlocked and locked the door that day are obtained.
  3. Output result: After the cycle ends, output the ID number of the door opening and closing records.

3.4 Points to note

The key to the algorithm is to use the string comparison function strcmp to compare times.

4. Reference answer

#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;
}

Guess you like

Origin blog.csdn.net/weixin_40171190/article/details/134746985