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

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

//1006 Sign In and Sign Out 
#include 
#include 
#include 
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 
#include 
#include 
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;
}

你可能感兴趣的:(PAT-甲级)