PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) 凌宸1642

PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) 凌宸1642

题目描述:

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.

译:每个输入文件包含一个测试用例,每个测试用例包含一天的记录。这个用例以一个正整数 M 开始,表示总的记录条数。接下来 M 行,格式如下:

ID_number Sign_in_time Sign_out_time

给出的时间格式为:HH:MM:SS , 并且 ID_number 是一个不超过 15 个字符的字符串。


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.

译:对于每个测试用例,在一行中输出这一天中开门的人和锁门的人的 ID number 。这两个 ID number 之间必须用一个空格分开。

注意:题目保证记录是符合实际的,意味着每个人的签到的时间必须比签退的时间更早,并且没有两个人同时签到或者签退。


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

The Idea:

第一种方法: 很明显的是使用结构体的排序。首先就想到了 sort 。对于sort的排序是很熟悉了,所以一次就 AC 了 。

第二种方法:由于题目中并没有指出 M 的具体范围(写者首先在第一种方法中内置 MAX1000000,结果爆内存了),所以就想着使用动态添加记录的。而需要排序,故选择了 map (map 既能按照 key 升序降序排列也能按照 value 升序降序排,为了简单且题目中说明 签到时间和签退时间不会重复,所以可以利用时间作为 key 值,ID_number 作为 value 值,这样就可以分别拿到 签到时间升序 和签退时间降序的 第一个keyvalue 值就是本题答案。)


The Codes —— sort:

#include<bits/stdc++.h>
using namespace std ;
#define MAX 10010
typedef struct node{
string name ;
string inTime ;
string outTime ;
}Node ;
Node peo[MAX] ;
int m ;
bool cmp1(Node a , Node b){
return a.inTime < b.inTime ; // 直接比较字符串的大小 , cmp1 表示按照签到时间先后排序
}
bool cmp2(Node a ,Node b){
return a.outTime > b.outTime ; // cmp2 表示按照签退时间倒序排序
}
int main(){
cin >> m ;
getchar() ;
for(int i = 0 ; i < m ; i ++){
cin >> peo[i].name >> peo[i].inTime >> peo[i].outTime ;
}
sort(peo , peo + m , cmp1);
string s1 = peo[0].name ;
sort(peo , peo + m , cmp2) ;
s1 += " " + peo[0].name ;
cout << s1 << endl ;
return 0 ;
}

The Codes —— map:

#include<bits/stdc++.h>
using namespace std;
map<string,string,less<string> > in ; // 按照 关键字升序排列的 map
map<string,string,greater<string> > out ; // 按照 关键字降序排列的 map
int m ;
string name , inTime , outTime , ans ;
int main(){
cin >> m ;
getchar() ; // 吸收输入 m 之后的 换行符
for(int i = 0 ; i < m ; i ++){
cin >> name >> inTime >> outTime ;
in[inTime] = name ; // 建立 签到时间 到 ID_number 的 map
out[outTime] = name ; // 建立 签退时间 到 人名 的 map
}
map<string , string ,less<string> >::iterator it = in.begin() ;// 关键字升序迭代器
map<string , string , greater<string> >::iterator it2 = out.begin() ;// 关键字降序迭代器
ans = it -> second ; // 最早签到的 ID_number
ans += " " + it2 -> second ; // 最晚签退的 ID_number
cout << ans << endl ;
return 0 ;
}
上一篇:centos 创建以日期为名的文件夹


下一篇:快照(Snapshot)