您的位置:首页 > 编程语言 > C语言/C++

[PAT甲级]1006. Sign In and Sign Out (25)(找出机房开门锁门的人)

2017-08-19 19:20 567 查看

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:40


Sample Output:

SC3021234 CS301133


题目大意:

给出n个人的名字,签到时间和退出时间,找出最早来和最晚走的人的名字,即开门人和锁门人

n为正整数,一个人签到时间一定小于退出时间,不会有同时签到的情况

思路:

刚开始我定义了结构体,自己电脑上运行没一点儿问题,提交显示编译错误,我实在找不出问题,就换了另一种方法

将时间换算成秒进行比较,AC

希望有人能看出问题的话,指导我一下,不胜感激

AC代码:

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main(){
int m;
scanf("%d", &m);
string unlock_ID="",lock_ID="";//开门的人,锁门的人
int unlock_time = 86400;//开门时间
int lock_time = 0;//锁门时间
for(int i=0; i<m; i++){
string ID_number;
cin >> ID_number;
int h1,h2,m1,m2,s1,s2;
scanf("%02d:%02d:%02d %02d:%02d:%02d", &h1, &m1, &s1, &h2, &m2, &s2);
int timeIn = h1*3600+m1*60+s1;//转化成秒
int timeOut = h2*3600+m2*60+s2;
if(timeIn <= unlock_time){//如果此人来得更早,此人开门,更新开门时间
unlock_ID = ID_number;
unlock_time = timeIn;
}
if(timeOut >= lock_time){//如果此人走得更晚,此人锁门,更新锁门时间
lock_ID = ID_number;
lock_time = timeOut;
}
}
cout << unlock_ID << " " << lock_ID;
return 0;
}


代码2:(自己电脑运行没问题,但是提交显示编译错误)

#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
struct time{
int hh;
int mm;
int ss;
};
bool isEarlier(time aa, time bb){
if(aa.hh<=bb.hh || (aa.hh==bb.hh&&aa.mm<=bb.mm) || (aa.hh==bb.hh&&aa.mm==bb.mm&&aa.ss<=bb.ss))
return true;
return false;
}
int main()
{
int m;
scanf("%d", &m);
string unlock_ID="",lock_ID="";//开门的人,锁门的人
time unlock_time,lock_time;//开门时间,锁门时间
unlock_time.hh = 23;
unlock_time.mm = 59;
unlock_time.ss = 59;
lock_time.hh = 0;
lock_time.mm = 0;
lock_time.ss = 0;
for(int i=0; i<m; i++){
string ID_number;
cin >> ID_number;
time a; time b;
scanf("%02d:%02d:%02d %02d:%02d:%02d", &a.hh, &a.mm, &a.ss, &b.hh, &b.mm, &b.ss);
if(isEarlier(a, unlock_time)){//如果此人来得更早,此人开门,更新开门时间
unlock_ID = ID_number;
unlock_time = a;
}
if(isEarlier(lock_time, b)){//如果此人走得更晚,此人锁门,更新锁门时间
lock_ID = ID_number;
lock_time = b;
}
}
cout << unlock_ID << " " << lock_ID;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息