题解 | #开门人和关门人#
开门人和关门人
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;
}

