1006 Sign In and Sign Out
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
思路分析:
1.直接使用string类
string类的大小比较原理:两个字符串自左向右逐个字符比较(按ASCII值大小),这两个字符串必须长度相同。
#include <iostream>
#include <string>
using namespace std;
int main()
{
int M;
cin>>M;
string startTime = "24:00:00",endTime = "00:00:00";
string st,et;
string sn,en;
for(int i = 0;i<M;i++)
{
string tempS,tempE,name;
cin>>name>>tempS>>tempE;
if(tempS<startTime)
{
sn = name;
startTime=tempS;
}
if(tempE>endTime)
{
en = name;
endTime = tempE;
}
}
cout<<sn<<" "<<en;
return 0;
}
2.根据输入的字符串,分割成时,分,秒,逐一进行比较
#include <iostream>
#include <string>
using namespace std;
string id[100];string timeS[100];string timeE[100];
int reTime(string str,int index);
int main()
{
int M;
cin>>M;
for(int i=0;i<M;i++)
cin>>id[i]>>timeS[i]>>timeE[i];
int time[6]; //最早的 时0分1秒2 最晚的 时3分4秒5
time[0] = reTime(timeS[0],0);
time[1] = reTime(timeS[0],3);
time[2] = reTime(timeS[0],6);
time[3] = reTime(timeE[0],0);
time[4] = reTime(timeE[0],3);
time[5] = reTime(timeE[0],6);
int sIndex = 0,eIndex = 0; //记录 最早和最晚的下标
for(int i = 0;i<M;i++)
{
if(reTime(timeS[i],0) < time[0])
{
time[0] = reTime(timeS[i],0);
sIndex = i;
}
else if(reTime(timeS[i],0) == time[0])
{
if(reTime(timeS[i],3) < time[1])
{
time[1] = reTime(timeS[i],3);
sIndex = i;
}
else if(reTime(timeS[i],03) == time[1])
{
if(reTime(timeS[i],6) < time[2])
{
time[2] = reTime(timeS[i],6);
sIndex = i;
}
}
}
if(reTime(timeE[i],0) > time[3])
{
time[3] = reTime(timeE[i],0);
eIndex = i;
}
else if(reTime(timeE[i],0) == time[3])
{
if(reTime(timeE[i],3) > time[4])
{
time[4] = reTime(timeE[i],3);
eIndex = i;
}
else if(reTime(timeE[i],03) == time[4])
{
if(reTime(timeE[i],6) > time[5])
{
time[5] = reTime(timeE[i],6);
eIndex = i;
}
}
}
}
cout<<id[sIndex]<<" "<<id[eIndex];
return 0;
}
//index 只能取 0 3 6表示 时 分 秒
int reTime(string str,int index)
{
return (int)(str[index]-'0')*10 + (int)(str[index+1] - '0');
}