PAT-basic-1028 人口普查 java c++

一、题目


某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。

这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过 200 岁的老人,而今天是 2014 年 9 月 6 日,所以超过 200 岁的生日和未出生的生日都是不合理的,应该被过滤掉。

输入格式:

输入在第一行给出正整数 ,取值在;随后  行,每行给出 1 个人的姓名(由不超过 5 个英文字母组成的字符串)、以及按 yyyy/mm/dd(即年/月/日)格式给出的生日。题目保证最年长和最年轻的人没有并列。

输出格式:

在一行中顺序输出有效生日的个数、最年长人和最年轻人的姓名,其间以空格分隔。

输入样例:

5
John 2001/05/12
Tom 1814/09/06
Ann 2121/01/30
James 1814/09/05
Steve 1967/11/20
 

输出样例:

3 Tom John

二、解析


表示年份有一个小技巧,18140906表示1814.9.6,

int totalLive = year*10000 + month*100 + day;

首先判断输入是否合法,合法再判断是否更新最年长或最年少的人的名字。

三、代码


java版,会超时,挺离谱的。

import java.io.*;
import java.util.Scanner;
public class Main {
    static final int  MIN_LIMIT = 18140906;
    static final int  MAX_LIMIT = 20140906;
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());

        Person youngest = new Person(null, Integer.MIN_VALUE);
        Person oldest =  new Person(null, Integer.MAX_VALUE);
        int count = 0;

        for (int i = 0; i < n; i++) {
            String[] str = br.readLine().split("\\s+");
            String name = str[0];
            String[] split = str[1].split("\\/");
            int year = Integer.parseInt(split[0]);
            int month = Integer.parseInt(split[1]);
            int day = Integer.parseInt(split[2]);

            int totalLive = year*10000 + month*100 + day;

            if(isOK(totalLive)){
                count++;
                if(totalLive < oldest.totalLive){
                    oldest.name = name;
                    oldest.totalLive = totalLive;
                }
                if(totalLive > youngest.totalLive){
                    youngest.name = name;
                    youngest.totalLive = totalLive;
                }
            }
        }
        if(count == 0){
            System.out.println(count);
        }else{
            System.out.print(count + " " + oldest.name + " " + youngest.name);
        }

    }
    public static boolean isOK(int cur){
        if(cur < MIN_LIMIT || cur > MAX_LIMIT)
            return false;
        return true;
    }
    static class Person {
        String name;
        int totalLive;

        public Person(String name, int totalLive) {
            this.name = name;
            this.totalLive = totalLive;
        }
    }
}

c++ ac: 

//
// Created by dongdong on 2023/4/10.
//
#include <cstring>
#include "iostream"

#define MIN_LIMIT 18140906
#define MAX_LIMIT 20140906

typedef struct P {
    char name[6];
    int totalLive;
};
int isOK(int cur){
    if(cur < MIN_LIMIT || cur > MAX_LIMIT)
        return 0;
    return 1;
}
int main(){
    int n;
    P youngest, oldest;
    char str1[6]="";
    strcpy(youngest.name, str1);
    youngest.totalLive = INT32_MIN;

    char str2[6] = "";
    strcpy(oldest.name, str2);
    oldest.totalLive = INT32_MAX;

    scanf("%d", &n);

    int count = 0;
    for(int i=0; i<n; i++){
        char name[6];
        int year, month, day;
        scanf("%s %d/%d/%d", name, &year, &month, &day);
        int tLive = year*10000 + month*100 + day;
        if(isOK(tLive)){
            count++;
            if(tLive < oldest.totalLive){
                strcpy(oldest.name, name);
                oldest.totalLive = tLive;
            }
            if(tLive > youngest.totalLive){
                strcpy(youngest.name, name);
                youngest.totalLive = tLive;
            }
        }
    }
    if(count == 0)
        printf("%d\n", count);
    else
        printf("%d %s %s", count, oldest.name, youngest.name);
    return 0;
}

 

posted @ 2023-04-12 16:32  正明小佐  阅读(21)  评论(0编辑  收藏  举报