题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1006
分析:用c++的类来做,定义俩个重载运算符即可。简单题。
只是因为vc6.0的bug,在使用友元函数定义重载运算符时可能会出错,这时头部不能这么写:
#include<iostream> #include<string> using namespace std;
#include<iostream> #include<string> using std::cin; using std::cout; using std::endl; using std::string;
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:40Sample Output:
SC3021234 CS301133
参考代码:
//#include<iostream> //using namespace std; #include<iostream> using std::cin; using std::cout; using std::endl; using std::string; #include<string> class Time { public: Time(){}; int hour; int min; int sec; void set(int h,int m,int s); friend bool operator > (Time &t1,Time &t2); friend bool operator < (Time &t1,Time &t2); //friend void operator = (Time &t1,Time &t2); }; /*void operator = (Time &t1,Time &t2) { t1.hour = t2.hour; t1.min = t2.min; t1.sec = t2.sec; }*/ bool operator > (Time &t1,Time &t2) { if(t1.hour > t2.hour) return true; else if(t1.hour == t2.hour) { if(t1.min > t2.min) return true; else if(t1.min == t2.min) { if(t1.sec > t2.sec) return true; else return false; } else return false; } else return false; } bool operator < (Time &t1,Time &t2) { if(t1.hour < t2.hour) return true; else if(t1.hour == t2.hour) { if(t1.min < t2.min) return true; else if(t1.min == t2.min) { if(t1.sec < t2.sec) return true; else return false; } else return false; } else return false; } void Time::set(int h,int m,int s) { hour = h; min = m; sec = s; } class Record { public: Record(){}; void set(string s); string num; Time enter; Time out; }; void Record::set(string s) { num = s; } int main() { int N; string num; int h,m,s; int flag_in,flag_out; Time t1,t2; t1.set(23,59,59); t2.set(0,0,0); char t; cin>>N; Record * R = new Record[N]; int i; for(i=0; i<N; i++) { cin>>num; R[i].num = num; //设置编号 cin>>h>>t>>m>>t>>s; //进入时间 R[i].enter.set(h,m,s); if(R[i].enter < t1 ) { flag_in = i; //t1 = R[i].enter; t1.set(h,m,s); } cin>>h>>t>>m>>t>>s; //离开时间 R[i].out.set(h,m,s); if(R[i].out > t2) { flag_out = i; //t2 = R[i].out; t2.set(h,m,s); } } cout<<R[flag_in].num<<" "<<R[flag_out].num<<endl; return 0; }
以下程序在vc上运行时正确的,但是提交到PAT后却提示编译错误。或许是不允许结构体中包含结构体元素。
#include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; struct time{ int h; int m; int s; }; struct Node{ string id; time in; time out; }; bool cmp1(Node a,Node b){ long t1 = a.in.h*3600 + a.in.m*60 + a.in.s; long t2 = b.in.h*3600 + b.in.m*60 + b.in.s; return t1 < t2; } bool cmp2(Node a,Node b){ long t1 = a.out.h*3600 + a.out.m*60 + b.out.s; long t2 = b.out.h*3600 + b.out.m*60 + b.out.s; return t1 > t2; } int main() { int n; cin>>n; int i; vector<Node> node(n); for(i=0; i<n; i++){ cin>>node[i].id; scanf("%d:%d:%d %d:%d:%d",&node[i].in.h,&node[i].in.m,&node[i].in.s,&node[i].out.h,&node[i].out.m,&node[i].out.s); } sort(node.begin(),node.end(),cmp1); cout<<node[0].id<<" "; sort(node.begin(),node.end(),cmp2); cout<<node[0].id<<endl; return 0; }
改为如下后可以通过:
#include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; struct record{ string number; int inH, inM, inS; int outH, outM, outS; }; bool cmpIn(record a, record b){ return (a.inH+a.inM*60+a.inS*3600) < (b.inH+b.inM*60+b.inS*3600); } bool cmpOut(record a, record b){ return (a.outH + a.outM*60 + a.outS*3600) > (b.outH + b.outM*60 + b.outS*3600); } int main(){ int cnt,i; cin>>cnt; vector<record> vec(cnt); for(i=0; i<cnt; i++){ cin>>vec[i].number; scanf("%d:%d:%d %d:%d:%d",&vec[i].inH, &vec[i].inM, &vec[i].inS, &vec[i].outH, &vec[i].outM, &vec[i].outS); } sort(vec.begin(),vec.end(),cmpIn); cout<<vec[0].number<<" "; sort(vec.begin(), vec.end(), cmpOut); cout<<vec[0].number<<endl; return 0; }