PAT甲级1006 Sign In and Sign Out(排序)

简单的排序题。对登录与登出时间分别做一次排序,分别输出最小、最大值就好了。吐槽一下题目没给m的数据范围。

由于登录登出时间都是格式化了的,且并不会有两个记录的登录或登出的时间是相等的情况,所以可以用string来存储,并直接利用> 、<来比较大小(不经意间就卖了个萌?)。而把登录登出时间转换为秒来比较大小也完全可以,只是有些麻烦了。

#include 
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
struct record
{
    string ID_number, Sign_in_time, Sign_out_time;
    record() {}
    record(string id, string in, string out): ID_number(id), Sign_in_time(in), Sign_out_time(out) {};
};
vector records;

void read()
{
    int m;
    cin >> m;
    for(int i = 0; i < m; ++i)
    {
        string id, in, out;
        cin >> id >> in >> out;
        records.push_back(record(id, in, out));
    }
}

bool cmp1(record r1, record r2)
{
    return r1.Sign_in_time < r2.Sign_in_time;
}

bool cmp2(record r1, record r2)
{
    return r1.Sign_out_time > r2.Sign_out_time;
}

void solve()
{
    sort(records.begin(), records.end(), cmp1);
    cout << records[0].ID_number << ' ';
    sort(records.begin(), records.end(), cmp2);
    cout << records[0].ID_number;
}

int main()
{
	read();
	solve();
	return 0;
}

 

你可能感兴趣的:(PAT甲级题解)