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 <iostream>
#include <string>
#include <vector>
#include <algorithm>
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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值