算法题练习系列之(二十一): 人口普查

xiaoxiao2021-02-28  89

--------------------------------------------------------------------------------------------------------------------------------------------------------

时间限制:1秒  空间限制:32768K  代码长度限制 100 KB

--------------------------------------------------------------------------------------------------------------------------------------------------------

题目描述

某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。 这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过200岁的老人, 而今天是2014年9月6日,所以超过200岁的生日和未出生的生日都是不合理的,应该被过滤掉。
输入描述:
输入在第一行给出正整数N,取值在(0, 105];随后N行,每行给出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

--------------------------------------------------------------------------------------------------------------------------------------------------------

实现思路:

(1).用Man类存储人的属性(姓名、生日的年月日); (2).用list存储人口信息,存储前判断该生日是否合理,判断方式就是年纪在0-200岁;

(3).对list进行升序排序;

(4).输出list大小,以及第一个和最后一个元素的信息即可;

--------------------------------------------------------------------------------------------------------------------------------------------------------

package com.biyao.algorithm.niuke.a1; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Scanner; public class Main_a1_018 { static class Man{ String name; int year; int month; int day; public Man(String name,int year,int month,int day){ this.name = name; this.year = year; this.month = month; this.day = day; } } public static void main(String[] args) { Scanner scan = new Scanner(System.in); while(scan.hasNext()){ int num = scan.nextInt(); List<Man> list = new ArrayList<Man>(); for (int i = 0; i < num; i++) { String name = scan.next(); String birth = scan.next(); String[] birthArr = birth.split("/"); int year = Integer.parseInt(birthArr[0]); int month = Integer.parseInt(birthArr[1]); int day = Integer.parseInt(birthArr[2]); if(isReasonableBirthday(year,month,day)){ Man m = new Man(name,year,month,day); list.add(m); } } Collections.sort(list, new Comparator<Man>(){ @Override public int compare(Man o1, Man o2) { if(o1.year > o2.year){ return 1; }else if(o1.year == o2.year){ if(o1.month > o2.month){ return 1; }else if(o1.month == o2.month){ if(o1.day > o2.day){ return 1; }else if(o1.day == o2.day){ return 0; }else{ return -1; } }else{ return -1; } }else{ return -1; } } }); System.out.println(list.size() + " " + list.get(0).name + " " + list.get(list.size()-1).name); } } public static boolean isReasonableBirthday(int year,int month,int day){ if(year < 1814 || year > 2014){ return false; }else if(year == 1814){ if(month < 9){ return false; }else if(month == 9){ if(day < 6){ return false; }else{ return true; } }else{ return true; } }else if(year == 2014){ if(month > 9){ return false; }else if(month == 9){ if(day > 6){ return false; }else{ return true; } }else{ return true; } } return true; } }

转载请注明原文地址: https://www.6miu.com/read-30682.html

最新回复(0)