zoukankan      html  css  js  c++  java
  • 1028. 人口普查(20)

    原题: https://www.patest.cn/contests/pat-b-practise/1028

    实现思路: 创建一个结构体, 分别包含name, year, month, person
    首先录入全部人口信息, 边录入边过滤掉不合理的生日. 最后遍历合理人口数据, 找到最年长的人和
    最年轻的人即可.
    坑1: 题目中明确说了, 不会出现并列. 但根据测试, 有可能出现人口为0的情况, 也就是测试点3,
    此时应该输出"0".

    完整代码:

    #include <stdio.h>
    
    struct person {
        char name[10];
        int year;
        int month;
        int day;
    };
    typedef struct person s_person;
    int tyear = 2014;   // 今年
    int tmonth = 9;     // 今月
    int tday = 6;       // 今日
    int maxAge = 200;   // 镇上最大年龄
    int i;
    
    int judge (s_person psn);
    int compare (s_person psn1, s_person psn2);
    
    int main (void) {
        int n;                       // 镇上总人口个数
        s_person allPeople[100000];  // 总人口
        int len = 0;         // 人口实际个数
        s_person temp;       // 临时保存人口信息
        char ch;             // 丢弃字符
        s_person youngest;   // 最年轻的人
        s_person oldest;     // 最年长的人
        scanf("%d", &n);
        for (i=1; i<=n; i++) {
            scanf(
                "%s %d%c%d%c%d",
                temp.name,
                &temp.year, &ch,
                &temp.month, &ch,
                &temp.day
            );
            if (judge(temp) == 1) {
                allPeople[len] = temp;
                len++;
            }
        }
    
        youngest = allPeople[0]; // 初始值
        oldest = allPeople[0];   // 初始值
        for (i=1; i<len; i++) {
            if (compare(youngest, allPeople[i]) == 1) {
                youngest = allPeople[i];
            }
            if (compare(oldest, allPeople[i]) == 2) {
                oldest = allPeople[i];
            }
        }
        
        if (len != 0) {
            printf("%d %s %s
    ", len, oldest.name, youngest.name);
        } else {
        // 这块这个坑, 个人感觉意义不大, 一个人都没有输出0, 这让我如何去想
            printf("0
    ");
        }
        
        return 0;
    }
    
    // 判断此人生日合不合理
    // 合理返回1, 不合理返回0
    int judge (s_person psn) {
        int minBirthYear = tyear - maxAge; // 最小出生年份
        // 年龄过大不合理
        if (psn.year < minBirthYear) {
            return 0;
        } else if (psn.year == minBirthYear && psn.month < tmonth) {
            return 0;
        } else if (
            psn.year == minBirthYear &&
            psn.month == tmonth &&
            psn.day < tday
        ) {
            return 0;
        } else if (psn.year > tyear) {
        // 年龄过小不合理
            return 0;
        } else if (psn.year == tyear && psn.month > tmonth) {
            return 0;
        } else if (
            psn.year == tyear &&
            psn.month == tmonth &&
            psn.day > tday
        ) {
            return 0;
        } else {
        // 其它合理情况
            return 1;
        }
    }
    
    // 判断2个人年龄相对大小(前提自然是2人生日均合理)
    // 相等返回0, 第1个人年龄大返回1, 第2人年龄大返回2
    // 实现思路, 排除1比2大和相等的情况, 剩下的就是1比2小
    int compare (s_person psn1, s_person psn2) {
        if (psn1.year < psn2.year) {
            return 1;
        } else if (psn1.year == psn2.year && psn1.month < psn2.month) {
            return 1;
        } else if (
            psn1.year == psn2.year &&
            psn1.month == psn2.month &&
            psn1.day < psn2.day
        ) {
            return 1;
        } else if (
            psn1.year == psn2.year &&
            psn1.month == psn2.month &&
            psn1.day == psn2.day 
        ) {
            return 0;
        } else {
            return 2;
        }
    }
    
  • 相关阅读:
    浅谈Chrome V8引擎中的垃圾回收机制
    selenium反爬机制
    03 HTTP协议与HTTPS协议
    HTTP缓存机制和原理
    python 自动发送邮件
    02 Anaconda的介绍,安装记以及使用
    01 关于jupyter的环境安装
    SQLAlchemy
    django-debug-toolbar
    flask 第十篇 after_request before_request
  • 原文地址:https://www.cnblogs.com/asheng2016/p/7823123.html
Copyright © 2011-2022 走看看