1028 人口普查java答案

╰半夏微凉° 2022-02-23 11:58 406阅读 0赞

这里用了输入流,并且继承了comparable的类进行排序,但是最后一个测试点仍然超时,暂时不清楚怎么解决

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.List;
  7. public class Main{
  8. public static void main(String[] args) throws IOException{
  9. BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
  10. int times=Integer.parseInt(bufferedReader.readLine());
  11. List<Person> people=new ArrayList<Person>();
  12. for(int i=0;i<times;i++) {
  13. String[] data=bufferedReader.readLine().split(" ");
  14. String[] time=data[1].split("/");
  15. int year=Integer.parseInt(time[0]);
  16. int month=Integer.parseInt(time[1]);
  17. int day=Integer.parseInt(time[2]);
  18. Person person=new Person(data[0],year,month,day);
  19. int age=person.getAge();
  20. // 年龄在0到200之间
  21. if(age>0&&age<200) {
  22. people.add(person);
  23. }else if(age==0||age==200) {
  24. // 年龄为0的和200的必须月份等于9,日期等于6
  25. if(month==9&&day==6) {
  26. people.add(person);
  27. }
  28. }
  29. }
  30. Collections.sort(people);
  31. if(people.size()!=0) {
  32. System.out.print(people.size()+" "+people.get(people.size()-1).getName()+" "+people.get(0).getName());
  33. }else {
  34. System.out.print(0);
  35. }
  36. }
  37. }
  38. class Person implements Comparable<Person>{
  39. private String name;
  40. private int year;
  41. private int month;
  42. private int day;
  43. private int age;
  44. public Person(String name,int year,int month,int day) {
  45. this.name=name;
  46. this.year=year;
  47. this.month=month;
  48. this.day=day;
  49. if(month<9) {
  50. age=2014-year;
  51. }else if(month>9) {
  52. age=2014-year-1;
  53. }else {
  54. if(day<=6) {
  55. age=2014-year;
  56. }else if(day>6) {
  57. age=2014-year-1;
  58. }
  59. }
  60. }
  61. public String getName() {
  62. return name;
  63. }
  64. public void setName(String name) {
  65. this.name = name;
  66. }
  67. public int getYear() {
  68. return year;
  69. }
  70. public void setYear(int year) {
  71. this.year = year;
  72. }
  73. public int getMonth() {
  74. return month;
  75. }
  76. public void setMonth(int month) {
  77. this.month = month;
  78. }
  79. public int getDay() {
  80. return day;
  81. }
  82. public void setDay(int day) {
  83. this.day = day;
  84. }
  85. public int getAge() {
  86. return age;
  87. }
  88. public void setAge(int age) {
  89. this.age=age;
  90. }
  91. @Override
  92. public int compareTo(Person person) {
  93. return this.age-person.age;
  94. }
  95. }

发表评论

表情:
评论列表 (有 0 条评论,406人围观)

还没有评论,来说两句吧...

相关阅读

    相关 1028 人口普查JAVA

    某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。 这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过 200 岁

    相关 1028 人口普查 (Python实现)

    某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。 这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过 200 岁

    相关 1028. 人口普查(20)

    某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。 这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过200岁的老

    相关 1028 人口普查

    题目描述   某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。   这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知

    相关 PAT-B 1028. 人口普查(20)

    某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。 这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过200岁的