目录
问题描述
大致意思就是,我们有这么一个系统,这个系统有很多用户,输入数据会给出用户账号,登陆时间和退出时间,要求我们找出这些登陆过的用户里面最早登录和最晚退出的都是谁,输出他们的账号即可。
思路
· 无非就是记录一下最小的时间值,最大的时间值以及他们对应的账号是什么。
我的想法是,就直接把这个时间字符串当成一个六位的整数,直接比较这个整数的大小就行了,如果直接比较字符串大小估计会出问题的,所以我是设置了一个权重值,让每一位数字乘以权重,最后加起来就行了。
比如例子中的:15:30:28 --------> 153028
这样直接比较数值,容易处理一些,然后我们每处理一个字符串就判断一下它是不是最小或最大,如果是,就更新你记录的最早登录账号或者最晚退出账号。
代码实现:
#include<iostream>
#include<string>
using namespace std;
/*
AC 一遍过 2022/11/30
PAT甲级1006 找出最早登录者和最晚退出者
*/
int main(){
int M,index;
string ID_number,Sign_in_time,Sign_out_time;
string earliest_ID,latest_ID;
long start_time,end_time;
long earliest = 10000000000,latest = 0;
cin>>M;
for(int i=0;i<M;i++){
index=1000000;
start_time=end_time=0;
cin>>ID_number>>Sign_in_time>>Sign_out_time;
for(int j=0;j<Sign_in_time.size();j++){
if(Sign_in_time[j]==':') continue;
start_time+=(Sign_in_time[j]-'0')*index;
end_time+=(Sign_out_time[j]-'0')*index;
index = index/10;
}
if(start_time<earliest) {
earliest = start_time;
earliest_ID = ID_number;
}
if(end_time>latest) {
latest = end_time;
latest_ID = ID_number;
}
}
cout<<earliest_ID<<" "<<latest_ID<<endl;
return 0;
}
运行效果