pat-1006 Sign In and Sign Out

1006:给出进入和离开时间,求最早来和最晚走的人

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


将时间转化成数字进行比较即可。
c++ 里貌似没有类似split的方便的函数,用find和substr辅助
find找不到时返回string::npos
c_str()转成一般数组。 c_str 和 length都是函数,不是属性,要加括号
substr函数有两个参数,一个是起始,另一个是长度,不是终止


#include<iostream>
using namespace std;
#include<string>

int time2int(string s)
{
string tmp = s;
int i = s.find(":");
int hour = atoi(tmp.substr(0,i).c_str());

tmp = tmp.substr(i+1,tmp.length());
i = s.find(":");
int minute = atoi(tmp.substr(0,i).c_str());

tmp = tmp.substr(i+1,tmp.length());
int sec = atoi(tmp.substr(0,i).c_str());

return hour*3600+minute*60+sec;
}


int main()
{
string unlock;
string lock;
int early=-1;
int late=-1;

int n;
string id;
string time1,time2;

cin>>n;
while(n--)
{
cin>>id;
cin>>time1;
cin>>time2;

int t =time2int(time1);
if(early==-1 || t < early)
{
early = t;
unlock = id;
}
t = time2int(time2);
if(late==-1 || t > late)
{
lock = id;
late = t;
}
}

cout<<unlock<<" "<<lock;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值