import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { // write your code here Scanner sc=new Scanner(System.in); int n=sc.nextInt(); ArrayList<String> name=new ArrayList<String>(); //ArrayList<String> list=new ArrayList<String>(); String[] date=new String[n]; //输入信息没毛病 for (int i=0;i<n;i++){ name.add(sc.next()); date[i]=sc.next(); } //把年月日合并成一个数组,先是字符型, String[] birth=new String[n]; String str; for (int i=0;i<n;i++){ str=""; for (int j=0;j<date[i].split("/").length;j++){ str=str+date[i].split("/")[j]; } birth[i]=str; } //再转成整型比较大小,对在合理范围内的数组进行处理。 int[] intbirth=new int[n]; int count=0; for (int i=0;i<n;i++){ intbirth[i]=Integer.parseInt(birth[i]); if (intbirth[i]>=18140906&&intbirth[i]<=20140906){ count++; } } //需要新建两个数组存储合理的姓名和生日 String[] namelegal=new String[count]; int[] birthlegal=new int[count]; int m=0; for (int i=0;i<n;i++){ if (intbirth[i]>=18140906&&intbirth[i]<=20140906){ if(m<count){ namelegal[m]=name.get(i); birthlegal[m]=intbirth[i]; m++; } } } //获取最年长者 System.out.print(count+" "); int max=birthlegal[0]; for(int i=0;i<count;i++){ if (max>birthlegal[i]){ max=birthlegal[i]; }else{ max=max; } } for(int i=0;i<count;i++){ if (max==birthlegal[i]){ System.out.print(namelegal[i]+" "); } } //获取最年轻者 int min=birthlegal[0]; for(int i=0;i<count;i++){ if (min<birthlegal[i]){ min=birthlegal[i]; }else{ min=min; } } for(int i=0;i<count;i++){ if (min==birthlegal[i]){ System.out.print(namelegal[i]); } } } }
1个格式错误,1个运行超时。
原文:https://www.cnblogs.com/zhuzehua/p/9784509.html