【PAT】1006. Sign In and Sign Out (25)

来源:互联网 发布:centos7源码编译 lnmp 编辑:程序博客网 时间:2024/05/07 16:30

题目链接: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:
3CS301111 15:30:28 17:00:10SC3021234 08:00:00 11:25:25CS301133 21:45:00 21:58:40
Sample 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;}



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 网上购物付款后商家不发货怎么办 夏商国际商城买到假货怎么办 重庆时时彩突然冻结账户资金怎么办 微信安全中心打不开是白色的怎么办 在淘宝买东西说好返现不给返怎么办 联通斐讯路由器返现被骗了怎么办 消费分期后退款分期账单还在怎么办 新房装修物业电梯用不了费用怎么办 在京东拼购没有拼成已付款的怎么办 微信上买东西发的货不一样怎么办 微信买东西收到货不付款怎么办 京东商品店家待出库不发货怎么办 微信二维码付款多付了怎么办 微信二维码付款付错了怎么办 身份证被移动公司拉黑了怎么办 微店申请退款卖家不同意怎么办 淘宝退货快递把我名字填错了怎么办 刚申请淘宝店信用为零怎么办 淘宝买家已付款卖家不做皮单怎么办 淘宝店铺被屏蔽7天后该怎么办 香信得登录密码忘了怎么办 苹果手机迅雷下载不了的资源怎么办 快手官方私信你的作品违规了怎么办 顺丰生鲜速配食物坏了怎么办 半年汽车没有年检 交警抓到怎么办 没年检的车子被交警抓到怎么办 去年检的路上被交警抓了怎么办 微信聊天界面群聊删除找不到怎么办 微博抽奖的奖品没发货怎么办 两个微信号绑了一个手机号怎么办 欧月玫瑰花朵叶子上有白粉怎么办 进对方空间被挡不想让他知道怎么办 qq上买东西给钱了对方没给怎么办 微信钱包手势密码忘记了怎么办 买家一起拍了两件宝贝怎么办 宿雾航空付款无法显示验证码怎么办 不小心把购物车的东西删了怎么办 微信在别人电脑登录忘退出怎么办 微信电脑版忘了退出怎么办 给微商交了定金不给退怎么办 方舟手游飞龙驯服时间不够怎么办