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;

}

你可能感兴趣的:(sign)