1006. Sign In and Sign Out (25)
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:40Sample Output:
SC3021234 CS301133
解法1:直接用sort函数整。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct E
{
char no[17];
char in[10];
char out[10];
}r[10000];
bool cmp1( E a , E b )
{
return strcmp( a.in,b.in ) < 0;
}
bool cmp2(E a,E b)
{
return strcmp(a.out,b.out)>0;
}
int main()
{
int n,i;
while(scanf( "%d" , &n )!=EOF)
{
for( i = 0 ; i < n ; i++ )
{
scanf( "%s%s%s" , r[i].no , r[i].in , r[i].out );
}
sort( r , r+n , cmp1 ); //按开始时间由小到大排
printf( "%s " , r[0].no );
sort( r , r+n , cmp2 ); //按结束时间由大到小排
printf( "%s\n" ,r[0].no );
}
return 0;
}
解法2:依次比较。
//把时间直接转化为秒,最有利于计算。。。。。。
//时间输入格式:必须要用%*c,否则0分。。。。。
#include<stdio.h>
#include<string.h>
int minTime=1000000,maxTime=0,size=0;
struct E{
char id[20];
int in;
int out;
}r[100],tmp1,tmp2; //结构体后有分号啊。。。tmp1保存最早来的人,tmp2保存最后走的人
int main()
{
int n,h1,m1,s1;
int i,h2,m2,s2;
char a[20];
//E tmp1,tmp2; //直接用名字,不带struct。。
while(scanf("%d",&n)!=EOF){
while(n--){
scanf("%s%d%*c%d%*c%d%d%*c%d%*c%d",a,&h2,&m2,&s2,&h1,&m1,&s1);//必须要用%*c否则0分。。。。。
strcpy(r[size].id,a); //把后面的赋给前面的。。字符串不能直接赋值!!
r[size].in=3600*h2+60*m2+s2;
r[size].out=3600*h1+60*m1+s1;
size++;
}
for(i=0;i<size;i++){
if(r[i].in<minTime){
minTime=r[i].in;
tmp1=r[i];
}
if(r[i].out>maxTime){
maxTime=r[i].out;
tmp2=r[i];
}
}
printf("%s %s\n",tmp1.id,tmp2.id);
}
return 0;
}