PAT-A 1006 Sign In and Sign Out(简单排序)

在这里插入图片描述
题目意思:
给出一组数据,包含学生进出实验室的时间,让你找出最先进去和最后走的那个人。
算法思想:
该题比较简单,将时间转为秒,便于相互之间的比较,在比较的过程中直接保存结果,最后输出结果即可。
这种题目有个坑,就是scanf是不能读入string类型的字符串的!
代码:

//1006 Sign In and Sign Out 
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int time(int hh, int mm, int ss)
{
	int comtime = hh * 3600 + mm * 60 + ss;
	return comtime;
}
int main()
{
	int m;
	char name[20], unlockname[20], lockname[20];
	int h1, h2, m1, m2, s1, s2, unlocktime=24*3600, locktime = 0;
	scanf("%d",&m);
	for(int i=0; i<m; i++)
	{
		scanf("%s %d:%d:%d %d:%d:%d",&name, &h1,&m1,&s1, &h2, &m2, &s2);
		int untime = time(h1,m1,s1);
		int locktimee = time(h2, m2, s2);
		if(untime < unlocktime)
		{
			unlocktime = untime;
			strcpy(unlockname, name);
		}
		if(locktimee > locktime)
		{
			locktime = locktimee;
			strcpy(lockname, name);
		}
	}
	printf("%s %s",unlockname, lockname);
	return 0;
}
//1006 Sign In and Sign Out (25分)
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int Inf = 24*3600;
int to_ss(int h, int m, int s)
{
	int time = h * 3600 + m * 60 + s;
	return time;
}
int n, ans_in=Inf, ans_ot=-1;
int main()
{
	int in_hh, in_mm, in_ss;
	int ot_hh, ot_mm, ot_ss;
	
	string id, sin_id, sot_id;	//分别是id,开门学生id,关门学生id 
	cin>>n;
	for(int i=0; i<n; i++)
	{
		getchar();
		cin>>id;
		scanf("%d:%d:%d %d:%d:%d",&in_hh,&in_mm,&in_ss,&ot_hh,&ot_mm,&ot_ss);
		int in_time = to_ss(in_hh, in_mm, in_ss);
		int ot_time = to_ss(ot_hh, ot_mm, ot_ss);
		if(in_time < ans_in)
		{
			ans_in = in_time;
			sin_id = id;
		}
		if(ot_time > ans_ot)
		{
			ans_ot = ot_time;
			sot_id = id;
		}
	}
	printf("%s %s",sin_id.c_str(), sot_id.c_str());
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值