题解 | #开门人和关门人#

开门人和关门人

https://www.nowcoder.com/practice/a4b37b53a44d454ab0834e1517983215

stoi方法,用于将字符串化为整数
#include <iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;


struct human {
	string cno;
	string time1;
	string time2;
};


bool compare1(human lhs, human rhs) {
	if (lhs.time1.substr(0, 2) == rhs.time1.substr(0, 2))
		return stoi(lhs.time1.substr(3, 2)) < stoi(rhs.time1.substr(3, 2));
	return lhs.time1.substr(0, 2) < rhs.time1.substr(0, 2);

}

bool compare2(human lhs, human rhs) {
	if (lhs.time2.substr(0, 2) == rhs.time2.substr(0, 2))
		return stoi(lhs.time2.substr(3, 2)) > stoi(rhs.time2.substr(3, 2));
	return lhs.time2.substr(0, 2) > rhs.time2.substr(0, 2);

}





int main() {
	int n;
	vector<human> vec;

	while (cin >> n) {
		vec.clear();
		for (int i = 0; i < n; i++)
		{
			human temp;
			cin >> temp.cno >> temp.time1 >> temp.time2;
			vec.push_back(temp);
		}
		sort(vec.begin(), vec.end(), compare1);
		cout << vec[0].cno << ' ';
		sort(vec.begin(), vec.end(), compare2);
		cout << vec[0].cno << endl;

	}

	

	return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
今天 12:10
点赞 评论 收藏
分享
05-23 19:02
吉林大学 Java
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务