[PTA] PAT(A) 1006 Sign In and Sign Out (25 分)

Problem

portal: 1006 Sign In and Sign Out

Description

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

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

Solution

Analysis

问题很简单,相当于在给定的一系列整数中找出最大值和最小值,不过就是数据类型不同于整型,而是一个其他格式的数。
创建一个结构体,名称为时间,写一个读取函数,一个比较函数。
开始读取输入,第一个人假设既是开门的人,又是关门的人,之后的人里面如果来的时间早于开门的人的时间,则将开门人的时间与编号进行更新,关门同理。

Code

#include <bits/stdc++.h>
using namespace std;

struct Time {
	int h, m, s;
	istream& operator>>(istream &is) {
		char ch;
		is >> h >> ch;
		is >> m >> ch;
		is >> s;
		return is;
	}
	bool operator<(Time t) {
		if (h != t.h) {
			return h < t.h;
		} else if (m != t.m) {
			return m < t.m;
		} else {
			return s < t.s;
		}
	}
};

istream& operator>>(istream &is, Time &t) {
	return t>>is;
}

int main(void) {
	string id, str;
	string unlock_id, lock_id;
	Time t1, t2, unlock_t, lock_t;
	int M;

	cin >> M;
	for (int i = 0; i < M; i++) {
		cin >> id;
		cin >> t1;
		cin >> t2;
		if (i == 0) {
			unlock_id = lock_id = id;
			unlock_t = t1;
			lock_t = t2;
			continue;
		}
		if (t1 < unlock_t) {
			unlock_id = id;
			unlock_t = t1;
		}
		if (lock_t < t2) {
			lock_id = id;
			lock_t = t2;
		}
	}
	cout << unlock_id << " " << lock_id << endl;
}

Result

原文地址:https://www.cnblogs.com/by-sknight/p/11413259.html