博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pat 1006 Sign In and Sign Out (25)
阅读量:5160 次
发布时间:2019-06-13

本文共 4097 字,大约阅读时间需要 13 分钟。

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:

3CS301111 15:30:28 17:00:10SC3021234 08:00:00 11:25:25CS301133 21:45:00 21:58:40

Sample Output:

SC3021234 CS301133 题目的输出是输出所有人中最早开门的和最晚锁门的人 解法一:很容易想到的是ID_number Sign_in_time Sign_out_time作为一个结构体的元素, struct info{
ID_number Sign_in_time Sign_out_time } vector
v; 将所有的信息存入容器中,利用sort函数,先根据Sign_in_time从小到大排序,输出第一个ID_number,再根据Sign_out_time从大到小排序,输出第一个ID_number。
1 #include 
2 #include
3 #include
4 #include
5 using namespace std; 6 struct info{ 7 string ID_number; 8 string Sign_in_time; 9 string Sign_out_time;10 };11 bool unlock(info lhs,info rhs){12 return lhs.Sign_in_time
rhs.Sign_out_time;16 }17 int main(int argc,char **argv){18 info I;19 string ID_number,Sign_in_time,Sign_out_time;20 string unlock_ID,lock_ID;21 vector
vi;22 int N;23 cin>>N;24 for(int i=0;i
>ID_number>>Sign_in_time>>Sign_out_time;26 I.ID_number=ID_number;I.Sign_in_time=Sign_in_time;I.Sign_out_time=Sign_out_time;27 vi.push_back(I);28 }29 std::sort(vi.begin(),vi.end(),unlock);30 unlock_ID=(*vi.begin()).ID_number;31 std::sort(vi.begin(),vi.end(),lock);32 lock_ID=(*vi.begin()).ID_number;33 cout<
<<" "<
<
C++字符串比较很给力!!

解法二:我只要所有人中Sign_in_time最小和Sign_out_time最大的,所以初始化所有人中unlock_time=”23:59:59“,lock_time=”00:00:00“

每输入一个ID_number Sign_in_time Sign_out_time,若Sign_in_time <= unlock_time,则unlock_time = Sign_in_time, In_ID_number  = ID_number,

若Sign_out_number >= lock_time,则lock_time = Sign_out_time, Out_ID_number  = ID_number,

 

1 #include 
2 #include
3 using namespace std; 4 5 int main(){ 6 char mins[16],maxs[16], str[16]; 7 char minT[10] = "23:59:59"; 8 char maxT[10] = "00:00:00"; 9 char tmp_in[10], tmp_out[10];10 int num, i;11 12 cin >> num;13 for(i = 0; i < num; i++){14 cin >> str >> tmp_in >> tmp_out;15 if(strcmp(tmp_in, minT) <= 0){16 strcpy(mins, str);17 strcpy(minT, tmp_in);18 }19 if(strcmp(tmp_out, maxT) >= 0){20 strcpy(maxs, str);21 strcpy(maxT, tmp_out);22 }23 }24 cout << mins << " " << maxs << endl;25 return 0;26 }

 

1 #include 
2 #include
3 #include
4 using namespace std; 5 6 int main(){ 7 string mins, maxs, str; 8 string minT = "23:59:59"; 9 string maxT = "00:00:00";10 string tmp_in, tmp_out;11 int num, i;12 13 cin >> num;14 for(i = 0; i < num; i++){15 cin >> str >> tmp_in >> tmp_out;16 if(tmp_in <= minT){17 mins = str;18 minT= tmp_in;19 }20 if(tmp_out >= maxT){21 maxs = str;22 maxT = tmp_out;23 }24 }25 cout << mins << " " << maxs << endl;26 return 0;27 }

C++字符串比较很给力!!

 

以前比较时间(   时:分:秒  )总是先比较小时,再比较分钟,最后比较秒,现在发现可以直接把它当做字符串,通过比较字符串来比较时间的先后。

 

 

 

 

 

转载于:https://www.cnblogs.com/qinduanyinghua/p/5709332.html

你可能感兴趣的文章
构建之法阅读笔记03
查看>>
ES5_03_Object扩展
查看>>
Apache-ab 接口性能测试
查看>>
EF 4.1 Code First Walkthrough
查看>>
常用MySQL语法
查看>>
bzoj 2600: [Ioi2011]ricehub
查看>>
创建数据库,表
查看>>
工厂模式
查看>>
计算机网络基础知识
查看>>
C#里如何遍历枚举所有的项
查看>>
如何在键盘出现时滚动表格,以适应输入框的显示
查看>>
超级强大的鼠标手势工具
查看>>
常用Dockerfile举例
查看>>
jquery的ajax用法
查看>>
设计模式-策略模式(Strategy)
查看>>
django orm 数据查询详解
查看>>
JarvisOJ Basic 熟悉的声音
查看>>
C# list导出Excel(二)
查看>>
CAS 单点登录模块学习
查看>>
跟着辛星用PHP的反射机制来实现插件
查看>>