PAT 1006 Sign In and Sign Out

原题目

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.

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

题目大意

每一天,第一个签到进入电脑室的人都会开门,最后一个签到离开电脑室的人都会锁门。给出签到记录,找到开门和锁门的人。
输入的第一行是一个正整数M,表示签到记录的条数。之后的M行按照ID、进入时间、出门时间排列。
输出开门和锁门的人的学号,中间用一个空格隔开。

题解

一开始捣鼓了半天C语言里char**应该怎么用动态内存分配和scanf输入,然后才猛然发现完全不需要将输入用数组存储。不过这个问题到现在还没完全解决。
一边读记录一边将时间字符串转换成整数(距00:00:00的秒数),记录进门时间最小和出门时间最大的学号,直接printf输出即可。

C语言代码如下:

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

int cal_sec(char time[9]){
    return ((time[0] - '0')*10 + time[1] - '0')*3600 + ((time[3] - '0')*10 + time[4] - '0')*60 + (time[6] - '0')*10 + time[7] - '0';
}

int main(){
    int m;
    char unlock_ID[17], lock_ID[17];
    char ID[17], sign_in_time[9], sign_out_time[9];
    int minn = 99999, maxx = 0;
    scanf("%d\n", &m);
    for(int i = 0;i < m;++i){
        scanf("%s %s %s", ID, sign_in_time, sign_out_time);
        int time = cal_sec(sign_in_time);
        if(time < minn){
            minn = time;
            strcpy(unlock_ID, ID);
        }
        time = cal_sec(sign_out_time);
        if(time > maxx){
            maxx = time;
            strcpy(lock_ID, ID);
        }
    }
    printf("%s %s\n", unlock_ID, lock_ID);
    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容