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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章