PAT (Advanced Level)-1006. Sign In and Sign Out (25)

PAT (Advanced Level)-1006. Sign In and Sign Out (25)

1006. Sign In and Sign Out (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue


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

解读

读完题目后,应该可以发现,这其实就是一个关于字符串大小比较的问题。 (将时间信息当做字符串处理)

因为采用了标准的24小时制时间输入,所以在比较两段时间时候,就是从第一位逐位进行大小比较,小的时间小,大的时间大,相同则比较下一位。

看到这块,应该能类比到字符串大小的比较吧,两者的处理方式是相同的。

也是采用从第一位开始,逐位比较大小,ASCII值小则字符串小,,ASCII值大则字符串大,相同则比较下一位。

又因为C++中的string类型可以直接使用<,>判断符进行string字符串的比较,所以我们在创建变量时候考虑这一点,在比较过程中可以更方便。

完整代码

#include<iostream>
#include<string>

using namespace std;

//创建结构体,来存储每次输入的员工记录ID和登入时间和登出时间
struct Record
{
	char id[15];
	string signInTime;
	string signOutTime;
};

int main()
{
    //题目中要求的变量M
	int M;
	cin >> M;

    //动态申请大小为M的结构体数组(数组的每个元素都是一个结构体)
	Record *recordList = new Record[M];
	
	//标记开门和关门的ID
	int unlockID=0, lockID=0;
	
	//比较大小时的临时变量
	string unlock, lock;
	
	//输入和比较同时进行
	for (int i = 0; i < M; i++)
	{
		cin >> recordList[i].id;
		cin >> recordList[i].signInTime;
		cin >> recordList[i].signOutTime;

		
        //初次赋值时候,初始化用来比较大小的临时量
		if (i == 0)
		{
			unlock = recordList[i].signInTime;
			lock = recordList[i].signOutTime;
		}
        
        //比较开门时间
		if (unlock > recordList[i].signInTime)
		{
			unlock = recordList[i].signInTime;
			unlockID = i;
		}
        
        //比较关门时间
		if (lock < recordList[i].signOutTime)
		{
			lock = recordList[i].signOutTime;
			lockID = i;
		}
	}

	cout << recordList[unlockID].id << " " << recordList[lockID].id;
	
	return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注