pat B 1028 人口普查

年龄计算与排序
本文介绍了一个使用C++实现的程序,该程序通过输入姓名和出生日期来计算参与者的年龄,并按年龄进行排序。参与者需提供姓名及出生年月日,程序会筛选有效数据并输出年龄最小和最大的参与者。

作死的选择算年龄,答非所问,不是好习惯,错了都不知哪里错了 - -、、、、、

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>

using namespace std;

struct birth{
    char *ch;
    int age;
};


int cmp(struct birth a,struct birth b){
    return a.age < b.age;
}
/*
int cmp(const void *aa,const void *bb){
    struct birth* a = (struct birth* ) aa;
    struct birth* b = (struct birth* ) bb;
    return a->age > b->age;
}
*/

int main(){
    int N;

 scanf("%d",&N);
            struct birth births[N];
            int cou = 0;
        for(int i = 0;i < N;i ++){
                int tempage = 0;
                char *ch1 = new char[6];
                char *ch2 = new char[11];
                scanf("%s %s",ch1,ch2);
                int month = 0;
                int day = 0;
                tempage = (ch2[0] - '0')*1000+(ch2[1] - '0')*100+(ch2[2] - '0')*10+(ch2[3] - '0');
                //printf("%d\n",tempage);
                if(tempage > 2014){
                    continue;
                }
                if(tempage < 1814){
                    continue;
                }

                tempage  = 2014 - tempage;
                month = (ch2[5] - '0') * 10 + (ch2[6] - '0');
                day = (ch2[8] - '0') * 10 + (ch2[9] - '0');
                //printf("%d %d\n",month,day);
                 if(tempage == 200&&month < 9){
                   continue;
                }
                if(tempage == 200&&month == 9&&day < 6){
                    continue;
                }


                    if(month > 9&&tempage == 0){
                        continue;
                    }

                    if(month == 9&&tempage == 0&&day > 6){
                        continue;
                    }

                    if(month > 9){
                        tempage ++;
                    }

                    if(month == 9){
                        if(day > 6){
                            tempage ++;
                        }
                    }




                    births[cou].ch = ch1;
                    births[cou].age = tempage;
                    cou++;
        }

        //qsort(births,cou,sizeof(birth),cmp);
        sort(births,births+cou,cmp);
        if(cou == 0)//若为0,输出为此格式。
        printf("0\n");
        else
        printf("%d %s %s\n",cou,births[cou - 1].ch,births[0].ch);


    return 0;
}

### 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、付费专栏及课程。

余额充值