甲级PAT 1006 Sign In and Sign Out

1006 Sign In and Sign Out (25)(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

题目要求: 

输入M个人一天内的进门以及出门时间,找出最早进门和最晚出门的人。输出他们的姓名,最后不能有空格

解题思路:

这里很明显用结构体排序很方便,第一种排序按进门时间从小到大,输出第一位的姓名。第二种排序按出门时间从小到大,输出最后一位的姓名。排序的比较是将HH:MM:SS对应的字符串HH,MM,SS通过stringstream转化为对应的数字。

注意:

使用sstream时有坑。刚开始使用stringstream只是每次转化后用了clear()函数以为就可以清空缓存,然而ss.clear(); 它并不清空任何内容,它只是重置了流的状态标志而已! ss.str(“”);  这样才是清空stringstream的缓冲,重复使用内存消耗也不再增加。

所以没有用ss.str(“”)之前:

《甲级PAT 1006 Sign In and Sign Out》

使用ss.str(“”)之后

《甲级PAT 1006 Sign In and Sign Out》

完整代码:(虽然很不简洁,但是简单哇)

#include<iostream>
#include<string>
#include<sstream>
#include<algorithm> 
using namespace std;
#define maxsize 1000

typedef struct Person{
	string name;
	string intime;
	string outtime;
}Person;


bool comp1(Person &p1,Person &p2){
	stringstream ss;
	int HH1,MM1,SS1,HH2,MM2,SS2;
	ss << p1.intime.substr(0,2);
	ss >> HH1;
	ss.clear();ss.str("");
	ss << p1.intime.substr(3,5);
	ss >> MM1;
	ss.clear();ss.str(""); 
	ss << p1.intime.substr(6,8);
	ss >> SS1;
	ss.clear();ss.str("");
	ss << p2.intime.substr(0,2);
	ss >> HH2;
	ss.clear();ss.str("");
	ss << p2.intime.substr(3,5);
	ss >> MM2;
	ss.clear(); ss.str("");
	ss << p2.intime.substr(6,8);
	ss >> SS2;
	if(HH1<HH2) return true;
	else if(HH1==HH2){
		if(MM1<MM2) return true;
		else if(MM1==MM2){
			if(SS1<SS2) return true;
			else return false;
		}
		else return false;
	} 
	else return false;
}

bool comp2(Person &p1,Person &p2){
	stringstream ss;
	int HH1,MM1,SS1,HH2,MM2,SS2;
	ss << p1.outtime.substr(0,2);
	ss >> HH1;
	ss.clear();ss.str("");
	ss << p1.outtime.substr(3,5);
	ss >> MM1;
	ss.clear();ss.str(""); 
	ss << p1.outtime.substr(6,8);
	ss >> SS1;
	ss.clear();ss.str("");
	ss << p2.outtime.substr(0,2);
	ss >> HH2;
	ss.clear();ss.str("");
	ss << p2.outtime.substr(3,5);
	ss >> MM2;
	ss.clear(); ss.str("");
	ss << p2.outtime.substr(6,8);
	ss >> SS2;
	if(HH1<HH2) return true;
	else if(HH1==HH2){
		if(MM1<MM2) return true;
		else if(MM1==MM2){
			if(SS1<SS2) return true;
			else return false;
		}
		else return false;
	} 
	else return false;
}

int main(){
	int M,i;
	Person p[maxsize];
	cin>>M;
	for(i=0;i<M;i++){
		cin>>p[i].name>>p[i].intime>>p[i].outtime;
	}
	sort(p,p+M,comp1);
	cout<<p[0].name<<" ";
	sort(p,p+M,comp2);
	cout<<p[M-1].name;
	return 0;
} 

 

    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞