主要是读取和判断时间大小,其他逻辑没啥问题,贴代码
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <string> 5 6 using namespace std; 7 8 struct timer { 9 int h, m, s; 10 timer(int _h,int _m,int _s):h(_h),m(_m),s(_s){} 11 }; 12 bool operator<(const timer& lhs, const timer& rhs) 13 { 14 return lhs.h < rhs.h || 15 !(rhs.h < lhs.h) && lhs.m < rhs.m ||//注意在小时相等的情况下比较分钟 16 //注意在小时和分钟相等的情况下比较秒 17 !(rhs.h < lhs.h) && !(rhs.m < lhs.m) && lhs.s < rhs.s; 18 } 19 struct student { 20 string id; 21 timer time_in, time_out; 22 student(const string& _id,const timer& i,const timer& o): 23 id(_id),time_in(i),time_out(o){} 24 }; 25 26 vector<student> vs; 27 bool less_f(const student& lhs, const student& rhs) 28 { 29 return lhs.time_in < rhs.time_in; 30 } 31 32 bool bigger_f(const student& lhs, const student& rhs) 33 { 34 return rhs.time_out < lhs.time_out; 35 } 36 37 int main(void) 38 { 39 int n; 40 cin >> n; 41 for (int i = 0;i < n;i++) 42 { 43 string id; 44 int h1, m1, s1, h2, m2, s2; 45 cin >> id; 46 scanf("%d%*c%d%*c%d", &h1, &m1, &s1); 47 scanf("%d%*c%d%*c%d", &h2, &m2, &s2); 48 vs.push_back(student(id, 49 timer(h1, m1, s1), timer(h2, m2, s2))); 50 } 51 sort(vs.begin(), vs.end(), less_f); 52 cout << vs[0].id << " "; 53 sort(vs.begin(), vs.end(), bigger_f); 54 cout << vs[0].id << endl; 55 56 return 0; 57 58 }