zoukankan      html  css  js  c++  java
  • PAT B1028 人口普查(20)

    课本AC代码

    #include <cstdio>
    
    struct person {
        char name[10];
        int yy, mm, dd;
    } oldest, youngest, left, right, temp;
    
    bool LessEqu(person a, person b) {
        if(a.yy != b.yy) return a.yy <= b.yy;
        else if(a.mm != b.mm) return a.mm <= b.mm;
        else return a.dd <= b.dd;
    }
    
    bool MoreEqu(person a, person b) {
        if(a.yy != b.yy) return a.yy >= b.yy;
        else if(a.mm != b.mm) return a.mm >= b.mm;
        else return a.dd >= b.dd;
    }
    
    void init() {
        youngest.yy = left.yy = 1814;
        oldest.yy = right.yy = 2014;
        youngest.mm = oldest.mm = left.mm = right.mm = 9;
        youngest.dd = oldest.dd = left.dd = right.dd = 6;
    }
    
    int main() {
        init();
        #ifdef ONLINE_JUDGE
        #else
            freopen("1.txt", "r", stdin);
        #endif // ONLINE_JUDGE
        int n, num = 0;
        scanf("%d", &n);
        for(int i = 0; i < n; i++) {
            scanf("%s %d/%d/%d", temp.name, &temp.yy, &temp.mm, &temp.dd);
            int n = 0, m = 0;
            if(MoreEqu(temp, left) && LessEqu(temp, right)) {
                num++;
                if(LessEqu(temp, oldest)) oldest = temp;
                if(MoreEqu(temp, youngest)) youngest = temp;
            }
        }
        if(num == 0) printf("0
    ");
        else printf("%d %s %s
    ", num, oldest.name, youngest.name);
        return 0;
    }
    
    

    自己的,两个没过

    #include <cstdio>
    #include <cstring>
    
    const int nowyear = 2014;
    const int nowmonth = 9;
    const int nowday = 6;
    const int oldyear = 2014 - 200;
    
    struct People {
        char name[50];
        int year, month, day;
    } temp, oldest, youngest, inleft, inright;
    
    bool right(People a, People b) {    //判断出生日期是否比现在早
        if(a.year == b.year) {
            if(a.month == b.month) {
                if(a.day == b.day) return true;
                else return b.day > a.day;
            } else return b.month > a.month;
        } else return b.year > a.year;
    }
    
    bool left(People a, People b) {       //判断出生日期是否大于最早日期
        if(a.year == b.year) {
            if(a.month == b.month) {
                if(a.day == b.day) return true;
                else return a.day > b.day; //a.day - b.day;
            } else return a.month > b.month; //a.month - b.month;
        }else return a.year > b.year; //a.year - b.year;
    }
    bool judge(People a) {      //判断日期是否合理
        if(left(inright, a) && right(inleft, a)) return true;
        else return false;
    }
    
    void init() {
        oldest.year = inright.year = nowyear;
        youngest.year = inleft.year = nowyear - 200;
        oldest.month = youngest.month = inright.month = inleft.month = nowmonth;
        oldest.day = youngest.day = inright.month = inleft.day = nowday;
    }
    
    int main() {
        #ifdef ONLINE_JUDGE
        #else
            freopen("1.txt", "r", stdin);
        #endif
        init();
        int n;  //生日的个数
        int a = 0;  //有效生日个数
        scanf("%d", &n);
        for(int i = 0; i < n; i++) {
            scanf("%s %d/%d/%d", temp.name, &temp.year, &temp.month, &temp.day);
            //printf("%s %d/%d/%d
    ", temp.name, temp.year, temp.month, temp.day);
            int n = 0, m = 0;
            //  n = right()
            if(judge(temp)) {
                a++;    //有效生日个数加1
                if(right(youngest, temp)) youngest = temp;
                if(left(oldest, temp)) oldest = temp;
                //printf("youngest:%s oldest:%s
    ", youngest.name, oldest.name);
            }
        }
        if(a == 0) printf("0
    ");
        else printf("%d %s %s", a, oldest.name, youngest.name);
        return 0;
    }
    
  • 相关阅读:
    vue技术分享之你可能不知道的7个秘密
    JVM知识总结-运行时区域划分
    如何使用加多宝(jdb)在linux下调试Java程序
    RabbitMQ 高可用之镜像队列
    Gson格式转换Integer变为Double类型问题解决
    IPv6地址表示方式
    MySQL truncate()函数的使用说明
    Java 实现判断 主机是否能 ping 通
    MySQL 性能优化系列之一 单表预处理
    Linux 查看CPU和内存的使用情况
  • 原文地址:https://www.cnblogs.com/isChenJY/p/11298645.html
Copyright © 2011-2022 走看看