PATB1028:人口普查

本文介绍PATB1028人口普查问题的解决方法,使用结构体存储个人信息,并通过多级排序算法筛选出有效日期范围内的记录。最终输出有效记录总数及最早和最晚出生的人名。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PATB1028:人口普查 【经典】 + 结构体的多级排序 与求最值 其实可以考虑 sort

【知识点】

结构体的多级比较 两次运用,一次是合法验证,一次是求最值比较

三步骤 :

1、结构体;

2、cmp,

3、【合法验证与比较】 (日期left < temp

    //重新完成B1028   思路 判断一个数

    //思路 : 输入日期 有效(日期min < temp < max) 就cout 然后比较 temp得到最值
    //
    struct Person{
        char name[10];
        int yy=1814, mm=12, dd=30;
    }temp1,old,young,left1,right1;
    //temp 临时比较的person,old实际上是出生日期最小的的人  young是出生日期最大的人哟 别乱了
    //left ,right 是边界 合法检测用 MoreEqu(temp,right)  求最值用 
MoreEqu(young,temp) 
    //关键点:多级比较日期 ;; 

    //需要初始化边间
    void init(){
    left1.yy = young.yy = 1814;
    right1.yy = old.yy = 2014;
    left1.mm = right1.mm = young.mm =old.mm= 9;
    left1.dd = right1.dd = young.dd = old.dd = 6;

}
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;
    }
}


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;
    }
}


void B1028(){
    init();
    int N;
    cin >> N;
    int count = 0;
    cin.get();
    for (int i = 0; i < N; i++)
    {
        cin >>temp1.name>>temp1.yy>>temp1.mm>>temp1.dd;
        if (MoreEqu(temp1, left1) && LessEqu(temp1, right1))
        {
            count++;
            if (LessEqu(young,temp1))
            {
                young=temp1;
            }
            if (MoreEqu(old,temp1))
            {
                old = temp1;
            }
        }
    }

    cout << count << " " << old.name << " " << young.name << endl;
}

【参考答案2】

【思路】:

结构体中做标记,flag 如果为true 就count++ ;

sort 之后 ,输出有序数组的最后和最前的人。

//====================================PATB1028 人口普查=====================

//人口普查  person有比较多的属性,所以需要构建struct 另外最最值,所以需要sort
const int maxn = 100000;
struct Person {
    bool heli=false;
    char name[15];
    int yy=1814, mm=12, dd=30;
}temp[maxn];

Person heliPerson[maxn];
bool paixu(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 pandan(Person p){
    bool panduan = (p.yy < 2014 && p.yy>1814);
    if(panduan){
        p.heli = true;
    }
    else if (p.yy==2014){
        if (p.mm<9){
            p.heli = true;
        }
        else if (p.mm==9){
            if (p.dd<=6)
            {
                p.heli = true;
            }
        }
    }
    else if (p.yy == 1814){
        if (p.mm > 9){
            p.heli = true;
        }
        else if (p.mm == 9){
            if (p.dd >= 6)
            {
                p.heli = true;
            }
        }
    }
}



void rkpcPATB1028(){
    int n;
    cin>>n;
    for (int i = 0; i < n; i++){
        cin>>temp[i].name>>temp[i].yy>>temp[i].mm>>temp[i].dd;
    }
    int count = 0;
    for (int i = 0; i < n; i++) {
        //=======================================
        bool panduan = (temp[i].yy < 2014 && temp[i].yy>1814);
        if (panduan){
            temp[i].heli = true;
        }
        else if (temp[i].yy == 2014){
            if (temp[i].mm<9){
                temp[i].heli = true;
            }
            else if (temp[i].mm == 9){
                if (temp[i].dd <= 6)
                {
                    temp[i].heli = true;
                }
            }
        }
        else if (temp[i].yy == 1814){
            if (temp[i].mm > 9){
                temp[i].heli = true;
            }
            else if (temp[i].mm == 9){
                if (temp[i].dd >= 6)
                {
                    temp[i].heli = true;
                }
            }
        }

        //=======================================
        if (temp[i].heli){
            heliPerson[count++] = temp[i];
        }
    }

    sort(heliPerson, heliPerson + count, paixu);
    cout << count <<" "<< heliPerson[count - 1].name <<" "<< heliPerson[0].name<<endl;

}
### Java 实现 PAT 乙级 1028 人口普查 对于PAT乙级1028人口普查问题,在Java中的实现可以遵循以下逻辑结构。此题旨在处理一系列人的出生日期,找出最早和最晚出生的人,并统计总人数。如果没有任何记录,则需输出特定的信息。 #### 数据读取与预处理 程序首先需要接收输入的数据量N,之后逐行读入每个人的姓名及其对应的出生日期。为了方便后续操作,建议将这些信息封装到一个类中以便管理[^3]。 ```java import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; class Person { String name; Date birthday; public Person(String name, String dateStr) throws ParseException { this.name = name; SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); this.birthday = sdf.parse(dateStr); } } ``` #### 主要功能实现 接下来定义主函数来完成核心业务逻辑:初始化变量用于保存最早的出生日期和个人信息、最新的出生日期和个人信息;遍历所有人对象列表更新上述两个极端值;最后按照题目要求格式化输出结果。 ```java public class Main { public static void main(String[] args) throws ParseException { Scanner scanner = new Scanner(System.in); int n = Integer.parseInt(scanner.nextLine()); List<Person> people = new ArrayList<>(); for (int i = 0; i < n; ++i){ String line = scanner.nextLine(); String[] parts = line.split(" "); people.add(new Person(parts[0], parts[1])); } if(n == 0){ System.out.println(0); return ; } Collections.sort(people, Comparator.comparing(p -> p.birthday)); Person oldestPerson = people.get(0); Person youngestPerson = people.get(people.size() - 1); System.out.printf("%d %s %s\n",n ,youngestPerson.name,oldestPerson.name ); } } ``` 这段代码实现了对给定数据的有效处理,能够正确识别并打印出最年轻的个体名、最年老的个体名以及参与调查者的总数。当没有有效条目时,仅显示数字零作为指示[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值