本题关键:两个比较的时间位数相同,所以可以用字典序排序
两个数 | 字典序比较 | 数值比较 |
---|---|---|
08 12 | 08 < 12 (0 < 1) | 8 < 12 |
8(没有前导0) 12 | 8 > 12 (8 > 1) | 8 < 12 |
y总code:
老师是在循环里面的判断条件写的if (!i || in_time < first_time)
!i:先把第一个人记录成结果
(好聪明啊hh)
#include<iostream>
using namespace std;
int main()
{
int n;
string id, in_time, out_time;
string first_id, last_id;
string first_time, last_time;
cin >> n;
for (int i = 0; i < n; i ++ )
{
cin >> id >> in_time >> out_time;
if (!i || in_time < first_time)
{
first_id = id;
first_time = in_time;
}
if (!i || out_time > last_time)
{
last_id = id;
last_time = out_time;
}
}
cout << first_id << ' ' << last_id << endl;
return 0;
}
我的code:
我是在循环外面 先把第一个人的出入时间记成结果,然后更新!
#include<iostream>
using namespace std;
int main()
{
int n;
string id, in_time, out_time;
string first_id, last_id;
string first_time, last_time;
cin >> n;
cin >> id >> first_time >> last_time;
first_id= id;
last_id = id;
for (int i = 1; i < n; i ++ )
{
cin >> id >> in_time >> out_time;
if (i && in_time < first_time)
{
first_id = id;
first_time = in_time;
}
if (i && out_time > last_time)
{
last_id = id;
last_time = out_time;
}
}
cout << first_id << ' ' << last_id << endl;
return 0;
}
nice