PAT乙级1028

题目链接

https://pintia.cn/problem-sets/994805260223102976/problems/994805293282607104

题解

这题跟那个德才论(PAT乙级1015)什么的差不多。

因为我用了string和algorithm,所以整个代码实现比较简单。

值得注意的的是,刚开始第3个测试点没过,报错Segmentation fault

网上查题解后,发现还是边界情况的问题(当所有输入都非法时就会数组越界,所以特殊处理一下即可,参考链接:https://blog.csdn.net/daniel960601/article/details/55261196)

// PAT BasicLevel 1028
// https://pintia.cn/problem-sets/994805260223102976/problems/994805293282607104

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;


class Person{
public:
    string name;
    string birthday;
    Person(string name, string birthday){
        this->name = name;
        this->birthday = birthday;
    }
};

bool personCmp(Person &p1, Person &p2);
int main()
{
    // 人数
    int n;
    cin >>n;

    // 最早日期和最晚日期
    string earliest = "1814/09/06";
    string latest = "2014/09/06";

    // 获取人的信息
    vector<Person> personVec;
    string name,birthday;
    for(int i=0;i<n;++i){
        cin >> name >> birthday;
        if (birthday >= earliest && birthday<= latest){
            personVec.push_back(Person(name,birthday));
        }
    }

    // 输出结果
    cout << personVec.size();
    if (personVec.size() > 0){
        // 对所有人进行排序
        sort(personVec.begin(), personVec.end(), personCmp);
        cout << ' ' << personVec.front().name << ' ' << personVec.back().name;
    }

    //system("pause");
    return 0;
}
bool personCmp(Person &p1, Person &p2)
{
    // 生日 升序(生日从小到大就是年龄从大到小)
    return p1.birthday < p2.birthday; 
}

作者:@臭咸鱼

转载请注明出处:https://www.cnblogs.com/chouxianyu/

欢迎讨论和交流!


原文地址:https://www.cnblogs.com/chouxianyu/p/11318426.html