pat签入与签出

思路:这题比较简单,我是先用三个数组来记录每一行的数据,然后用标志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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值