【PAT-A1006】 Sign In and Sign Out (25分)(c++)

这个题的就是让找最早到的人与最晚离开的人。
我使用的核心实现方法就是前两天做的一道题目——PAT-B1028】人口普查 (20分)(c++)

#include <iostream>
#include <stdio.h>
using namespace std;

struct INandOUT
{
	string name;
	int in_hh, in_mm, in_ss;
	int out_hh, out_mm, out_ss;
}temp, first, last;


//初始化
void init()
{
	//找最开始的
	first.in_hh = first.in_mm = first.in_ss = 99;
	//找最后的
	last.out_hh = last.out_mm = last.out_ss = 00;
}

//当前的比设定的更考前
bool First(INandOUT temp,INandOUT first)
{
	if (temp.in_hh != first.in_hh) return temp.in_hh < first.in_hh;
	else if (temp.in_mm != first.in_mm) return temp.in_mm < first.in_mm;
	else if (temp.in_ss != first.in_ss) return temp.in_ss < first.in_ss;
}


//当前的比设定的靠后
bool Last(INandOUT temp, INandOUT first)
{
	if (temp.out_hh != last.out_hh) return temp.out_hh > last.out_hh;
	else if (temp.out_mm != last.out_mm) return temp.out_mm > last.out_mm;
	else if (temp.out_ss != last.out_ss) return temp.out_ss > last.out_ss;
}

void test()
{
	int n;
	cin >> n;

	string name;
	for (int i = 0; i < n; i++)
	{
		cin >> name;
		scanf("%d:%d:%d", &temp.in_hh, &temp.in_mm, &temp.in_ss);
		scanf("%d:%d:%d", &temp.out_hh, &temp.out_mm, &temp.out_ss);

		//找到第一个
		if (First(temp, first))
		{
			//cout << "当前比设定的还要小" << endl;
			//更新最小
			first = temp;
			first.name = name;
		}
		else {
			
			//cout << "设定的就是最小的" << endl;
		}
		//找到最后一个
		if (Last(temp, last))
		{
			//cout << "当前的比设定的要迟" << endl;
			//更新最迟
			last = temp;
			last.name = name;
		}
		else
		{
			//cout << "设定的就是最迟的" << endl;
		}
		
	}

	cout << first.name << " " << last.name;

}

int main()
{
	init();
	test();

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值