At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in's and out's, you are supposed to find the ones who have unlocked and locked the door on that day.
Input Specification:
Each input file contains one test case. Each case contains the records for one day. The case starts with a positive integer M, which is the total number of records, followed by M lines, each in the format:
ID_number Sign_in_time Sign_out_time
where times are given in the format HH:MM:SS, and ID_number is a string with no more than 15 characters.
Output Specification:
For each test case, output in one line the ID numbers of the persons who have unlocked and locked the door on that day. The two ID numbers must be separated by one space.
Note: It is guaranteed that the records are consistent. That is, the sign in time must be earlier than the sign out time for each person, and there are no two persons sign in or out at the same moment.
Sample Input:
3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40
Sample Output:
SC3021234 CS301133
最普通版本
由于对C和C++字符串的使用有些生疏了,导致费了不少时间。(ps:C++中string字符串不能用printf输出
#include<cstdio>
#include<string.h>
using namespace std;
int main()
{
int n,i;
int h1,m1,s1,h2,m2,s2;
char idS[20],comeS[20],goS[20];
scanf("%d",&n);
//为了存取最大和最小时间,先设置方便进行比较的初始化对象
int minH = 24,minM = 60,minS = 60;
int maxH = 0,maxM = 0,maxS = 0;
for(i=0;i<n;i++)
{
scanf("%s%d:%d:%d%d:%d:%d",&idS,&h1,&m1,&s1,&h2,&m2,&s2);
//先比较小时,若相等比较分钟,最后是秒数。三种情况。
if(h1<minH||(h1==minH&&m1<minM)||(h1==minH&&m1==minM&&s1<minS))
{
//将最小时间的id存取在comeS中,并更新时分秒的最小值们
strcpy(comeS,idS);
minH = h1;
minM = m1;
minS = s1;
}
if(h2>maxH||(h2==maxH&&m2>maxM)||(h2==maxH&&m2==maxM&&s2>maxS))
{
strcpy(goS,idS);
maxH = h2;
maxM = m2;
maxS = s2;
}
}
printf("%s %s",comeS,goS);
}
结构体+排序代码:
#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
char id[20];
int h1,m1,s1;
int h2,m2,s2;
}stu[10005];
//注意一下,这里用的是结构体的名字node而不是stu
bool cmp2(node a,node b)
{
if(a.h2!=b.h2) return a.h2>b.h2;
else if(a.m2!=b.m2) return a.m2>b.m2;
return a.s2>b.s2;
}
bool cmp1(node a,node b)
{
if(a.h1!=b.h1) return a.h1<b.h1;
else if(a.m1!=b.m1) return a.m1<b.m1;
return a.s1<b.s1;
}
int main()
{
int m,i;
scanf("%d",&m);
for(i=0;i<m;i++)
scanf("%s%d:%d:%d %d:%d:%d",&stu[i].id,&stu[i].h1,&stu[i].m1,&stu[i].s1,&stu[i].h2,&stu[i].m2,&stu[i].s2);
sort(stu,stu+m,cmp1);
printf("%s ",stu[0].id);
sort(stu,stu+m,cmp2);
printf("%s",stu[0].id);
return 0;
}
时分秒格式的大小比较
全部转化成秒,统一格式以后,直接比较就可以啦~(ps:这是参考的柳神写法。
#include<cstdio>
#include<cstring>
int main()
{
int i,m,hh,mm,ss;
int comeTime = 24*3600+60*60+60+5,goTime = 0;
char id[20],come[20],go[20];
scanf("%d",&m);
for(i=0;i<m;i++)
{
scanf("%s%d:%d:%d ",&id,&hh,&mm,&ss);
int newTime = hh*3600+mm*60+ss;
if(newTime<comeTime)
{
comeTime = newTime;
strcpy(come,id);
}
scanf("%d:%d:%d",&hh,&mm,&ss);
newTime = hh*3600+mm*60+ss;
if(newTime>goTime)
{
goTime = newTime;
strcpy(go,id);
}
}
printf("%s %s",come,go);
return 0;
}
本文介绍了一种用于记录计算机房进出人员的算法,通过分析输入输出记录来确定每天第一个进入和最后一个离开的人。该算法首先读取总记录数,然后逐条处理每条记录,包括员工ID和进出时间,最终输出解锁和锁定门的人员ID。
511

被折叠的 条评论
为什么被折叠?



