PAT 1006 (Advanced Level) Sign In and Sign Out

PAT 1006 Sign In and Sign Out

题目:

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

解题思路:

其实本体可以采用时间函数作弊,但是那不是本题所要考察的目的

  • 本题主要思想在于如何分割时间字符串,然后进行比较;
  • 首先利用结构体将person的信息存入其中,使用stoi函数,由于时间字符串的结构是一致的,所以对字符串使用substr进行分割,取出时分秒并进行比较
  • 以求最早进入者为例,最晚出去者类似:先比较小时,如果当前成员的小时数比当前最小的成员的小时数要小,则更新指针,否则如果相等,则继续判断分,再判断秒,在依次比较过之后,更新当前最早的人的指针(其实如果指针没变,则进行了一次无意义的赋值操作)
代码:
#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;
typedef struct{
	string person_id;
	string sign_in_time;
	string sign_out_time;
}Person;
int main(){
	int person_num;
	Person persons[110];
	scanf("%d",&person_num);
	int i;
	for(i=0;i<person_num;i++){
		cin >>persons[i].person_id >> persons[i].sign_in_time >> persons[i].sign_out_time;
	}
	if(person_num == 0){
		return 0;
	}
	//分别比较所有值中最大的和最小的
	string earliest = persons[0].person_id,least = persons[0].person_id;
	int c_point = 0;
	int m_point = 0;
	int in_hour = stoi(persons[0].sign_in_time.substr(0,2));
	int in_mins = stoi(persons[0].sign_in_time.substr(3,2));
	int in_secs = stoi(persons[0].sign_in_time.substr(6,2));
	int out_hour = stoi(persons[0].sign_out_time.substr(0,2));
	int out_mins = stoi(persons[0].sign_out_time.substr(3,2));
	int out_secs = stoi(persons[0].sign_out_time.substr(6,2));
	for(i=0;i<person_num;i++){
		if((stoi(persons[i].sign_in_time.substr(0,2))) < in_hour){    //找最早的进入者 
			c_point = i;
		}else if((stoi(persons[i].sign_in_time.substr(0,2))) == in_hour){
			if(stoi(persons[i].sign_in_time.substr(3,2)) < in_mins){
				c_point = i;
			}else if(stoi(persons[i].sign_in_time.substr(3,2)) == in_mins){
				if(stoi(persons[i].sign_in_time.substr(6,2)) < in_secs){
					c_point = i;
				}
			}
		}
		//更新当前进入的时间信息
		 in_hour = stoi(persons[c_point].sign_in_time.substr(0,2));
		 in_mins = stoi(persons[c_point].sign_in_time.substr(3,2));
		 in_secs = stoi(persons[c_point].sign_in_time.substr(6,2));
		 
		 if((stoi(persons[i].sign_out_time.substr(0,2))) > out_hour){    //找最晚的出去者 
			m_point = i;
		 }else if((stoi(persons[i].sign_out_time.substr(0,2))) == out_hour){
			if(stoi(persons[i].sign_out_time.substr(3,2)) > out_mins){
				m_point = i;
			}else if(stoi(persons[i].sign_out_time.substr(3,2)) == out_mins){
				if(stoi(persons[i].sign_out_time.substr(6,2)) > out_secs){
					m_point = i;
				}
			}
		 }
		 //更新当前出去的时间信息
		 out_hour = stoi(persons[m_point].sign_out_time.substr(0,2));
		 out_mins = stoi(persons[m_point].sign_out_time.substr(3,2));
		 out_secs = stoi(persons[m_point].sign_out_time.substr(6,2));
	}
	cout <<  persons[c_point].person_id << " " << persons[m_point].person_id;
	return 0;
}
发布了17 篇原创文章 · 获赞 0 · 访问量 467

猜你喜欢

转载自blog.csdn.net/chizhonghang/article/details/104044661