PAT(A) 1006. Sign In and Sign Out (25)

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

Sample Output:

SC3021234 CS301133
#include <cstdio>
#include <cstring>

//划重点(1):结构体内找最大最小 great()
struct pNode{
   char id[20]; //string id; //要报错 int h, m, s; }tmp, ans1, ans2; //ans1存放最早签到时间 ans2存放最晚签离时间 bool great(pNode node1, pNode node2){ //node1的时间 > node2的时间 则返回true
   if(node1.h != node2.h) return node1.h>node2.h; if(node1.m != node2.m) return node1.m>node2.m; return node1.s>node2.s; } int main() { int M=10; //划重点(2):C语音带格式的文件输入 //FILE *fp; //fp=fopen("test.txt", "r"); //fscanf(fp, "%d", &M); scanf("%d", &M); //划重点(3):找最小,初始设为最大 ans1.h=24, ans1.m=60, ans1.s=60; //初始签到时间设为最大 ans2.h=0, ans2.m=0, ans2.s=0; //初始签离时间设为最小 for(int i=0; i<M; i++){ //先读入ID和签到时间 scanf("%s%d:%d:%d", &tmp.id, &tmp.h, &tmp.m, &tmp.s); if(great(tmp, ans1) == false) ans1=tmp; //取更小的签到时间(整个结构体赋值) //再读入签离时间 scanf("%d:%d:%d", &tmp.h, &tmp.m, &tmp.s); if(great(tmp, ans2) == true) ans2=tmp; //取更大的签离时间 } printf("%s %s", ans1.id, ans2.id); return 0; }

 

posted @ 2017-03-14 13:53  claremz  阅读(186)  评论(0编辑  收藏  举报