PAT|1028 人口普查(简单模拟)

注意点:1.最年长人的出生日期是1814/9/6 

               2.最年轻人的出生日期是2014/9/6 

               3.存在输入样例均不在合理日期内 应只输出0

思路:将符合条件的加入进结构体数组中,并对他们进行排序,输出最大最小即可。

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <vector>
using namespace std;
const int maxn = 0x3f3f3f3f;
typedef long long ll;
struct Node{
    string a;
    int b,c,d;
}w[100005];
bool cmp(Node p,Node q) {
    if(p.b != q.b) return p.b < q.b;
    if(p.c != q.c) return p.c < q.c;
    if(p.d != q.d)  return p.d < q.d;
    return p.a < q.a;
}
int main() {
    int n;
    cin >> n;
    string a;
    int b,c,d;
    int t = 0;
    for(int i = 1; i <= n; i++) {
        cin >> a;
        scanf("%d/%d/%d",&b,&c,&d);

        if((b == 2014 && c == 9 && d >6) || (b == 1814 && c == 9 && d < 6) || (b == 2014 && c > 9 ) ||(b > 2014) ||(b == 1814 && c < 9)||(b < 1814)) {
                continue;
        }
        else {
            w[t].a = a;
            w[t].b = b;
            w[t].c = c;
            w[t++].d = d;
        }
        //
    }
    sort(w,w+t,cmp);
    cout << t;
    if(t != 0)
    cout << " " << w[0].a << " " << w[t-1].a<<endl;
    return 0;
}

posted on 2020-01-16 11:31  一只小毛球  阅读(226)  评论(0编辑  收藏  举报

导航