pat-1006 Sign In and Sign Out

1602 ワード

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関数には2つのパラメータがあります.1つは開始で、もう1つは長さで、終了ではありません.

#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;

}