思路:这题比较简单,我是先用三个数组来记录每一行的数据,然后用标志max1,max2来记录最早时间的数组下标和最晚时间的数组下标;时间的比较可以直接用字符串比大小,因为string可以按字典序列比较大小。
#include<iostream>
#include <string>
using namespace std;
int main()
{
int n;
cin >> n;
string s[11],pretime[11],lasttime[11];
for (int i = 1; i <= n; i++)
{
cin >> s[i] >> pretime[i] >> lasttime[i];
}
int max1=1,max2=1;
for (int i = 1; i <= n; i++)
{
if (pretime[max1] > pretime[i])
max1 = i;
if (lasttime[max2] < lasttime[i])
max2 = i;
}
cout << s[max1] <<" "<< s[max2] << endl;
return 0;
}
更优解:(看y总的)
省去了数组的空间,直接在第一次循环进行比较记录。
#include<iostream>
#include <string>
using namespace std;
int main()
{
int n;
cin >> n;
string open_id,close_id, open_time, close_time;
for (int i = 0; i < n; i++)
{
string id_name, in_time, out_time;
cin >> id_name >> in_time >> out_time;
if (!i || in_time < open_time)
{
open_id = id_name;
open_time = in_time;
}
if (!i || out_time > close_time)
{
close_id = id_name;
close_time = out_time;
}
}
cout << open_id <<" "<< close_id << endl;
return 0;
}