1006 Sign In and Sign Out (25分) 简单通俗解法

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

心得

这个题是比较时间前后的,我对时间进行的时分秒的分别对比,然后通过algorithm 里面的sort函数对其排序,其实完全没有必要,可以把时和分化成秒,最后比秒数的多少,这样会简单很多。

错误

在sort函数里面 的if条件 中间的 “=”号 是两个“==”,不要再写错了。这个错误也太der了。

关于sort排序

people是个结构体,第一个参数是首地址,vector用begin表示。第二个是首地址加长度。第三个是sort函数。

sort(people.begin(), people.begin() + people.size(), SortByIn);

比如下面这个sort函数,return的就是你想要排序的。只有当不符合普通排序条件的时候,才需要写下一个,如本题,不符合条件的只有当两个相等的时候,无法排序。

bool SortByIn(People people1, People people2) {
     
	if (people1.signIn1 == people2.signIn1)
		if (people1.signIn2 == people2.signIn2)
			return people1.signIn3 < people2.signIn3;
		else 	
			return people1.signIn2 < people2.signIn2;
	else 
		return people1.signIn1 < people2.signIn1;

		
}
#include 
#include 
#include 
#include 
using namespace std;

typedef struct {
     
	string ID;
	int signIn1 = 0; int signIn2 = 0; int signIn3 = 0;
	int signOut1 = 0; int signOut2 = 0; int signOut3 = 0;
}People;

int StringToInt(string a) {
     
	int b = (a[0] - '0') * 10 + (a[1] - '0');
	return b;
}

bool SortByIn(People people1, People people2) {
     
	if (people1.signIn1 == people2.signIn1)
		if (people1.signIn2 == people2.signIn2)
			return people1.signIn3 < people2.signIn3;
		else 	
			return people1.signIn2 < people2.signIn2;
	else 
		return people1.signIn1 < people2.signIn1;

		
}

bool SortByOut(People people1, People people2) {
     
	if (people1.signOut1 == people2.signOut1)
		if (people1.signOut2 == people2.signOut2)
			return people1.signOut3 > people2.signOut3;
		else
			return people1.signOut2 > people2.signOut2;
	else
		return people1.signOut1 > people2.signOut1;
}
vector<People> people;

int main() {
     
	int M;
	cin >> M;
	for (int i = 0; i < M; i++)
	{
     
		People peo;
		string ID, SignIn, SignOut;
		cin >> ID >> SignIn >> SignOut;
		peo.ID = ID;
		peo.signIn1 = StringToInt(SignIn.substr(0,2));
		peo.signIn2 = StringToInt(SignIn.substr(3, 2));
		peo.signIn3 = StringToInt(SignIn.substr(6, 2));
		peo.signOut1 = StringToInt(SignOut.substr(0, 2));
		peo.signOut2 = StringToInt(SignOut.substr(3, 2));
		peo.signOut3 = StringToInt(SignOut.substr(6, 2));
		people.push_back(peo);
	}
	sort(people.begin(), people.begin() + people.size(), SortByIn);
	cout << people[0].ID<<" ";
	sort(people.begin(), people.begin() + people.size(), SortByOut);
	cout << people[0].ID;
	return 0;
}

你可能感兴趣的:(PAT真题,算法,PAT,真题)