Loading

1006 Sign In and Sign Out (25 分)

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

2. 题意

有M个学生回来机房,每个学生对应一个进机房和出机房时间,找出进机房时间最早和出机房时间最晚的那两位学生。

3. 思路——简单模拟

  1. 输入第一个学生作为基准,将其进机房时间作为开门时间,出机房时间作为关门时间。
  2. 后面学生根据该基准,如果进入时间比其早,则更新开门时间;如果出机房时间比其晚,则更新关门时间。

注:这里的时间比较,因为时间格式严格规定,所以可以直接通过字符串比较来得到时间大小。

4. 代码

#include <iostream>
#include <string>

using namespace std;

// 这里时间先后比较,直接使用字符串比价即可 
int main()
{
	int n;
	cin >> n;
	// 如果n小于等于0,说明没有其余输入 
	if (n <= 0) return 0;
	string name1, name2;
	string time1, time2;
	cin >> name1 >> time1 >> time2;
	name2 = name1;
	n -= 1;
	string idNum, inTime, outTime; 
	while (n--)
	{
                cin >> idNum >> inTime >> outTime;
		// 比较当前用户进机房时间和开门时间,如果较小,则更新开门时间 
		if (inTime.compare(time1) < 0)
		{
			time1 = inTime;
			name1 = idNum;
		} 
		// 比较当前用户出机房时间和关门时间,如果较大,则更新关门时间 
		if (outTime.compare(time2) > 0)
		{
			time2 = outTime;
			name2 = idNum;
		}
	}
	cout << name1 << " " << name2 << endl;
	return 0;
} 
posted @ 2021-10-28 22:08  August_丶  阅读(21)  评论(0编辑  收藏  举报