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

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

输入格式:

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

输出格式:

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

输入样例:

  1. 5
  2. John 2001/05/12
  3. Tom 1814/09/06
  4. Ann 2121/01/30
  5. James 1814/09/05
  6. Steve 1967/11/20

输出样例:

  1. 3 Tom John

思路

这题面向对象思想浓厚,第一次用了Java来写

第4个测试点没过。。。。

第二次回顾用C++写,代码AC。


代码

  1. import java.util.Scanner;
  2. class Person {
  3. public String name;
  4. public int year;
  5. public int month;
  6. public int day;
  7. public Person() {
  8. }
  9. public Person(String name, int year, int month, int day) {
  10. this.name = name;
  11. this.year = year;
  12. this.month = month;
  13. this.day = day;
  14. }
  15. public boolean Bigger(Person left) {
  16. if(this.year != left.year)
  17. return this.year >= left.year;
  18. else if(this.month != left.month)
  19. return this.month >= left.month;
  20. else
  21. return this.day >= left.day;
  22. }
  23. public boolean Smaller(Person right) {
  24. if(this.year != right.year)
  25. return this.year <= right.year;
  26. else if(this.month != right.month)
  27. return this.month <= right.month;
  28. else
  29. return this.day <= right.day;
  30. }
  31. }
  32. public class Main {
  33. public static void main(String[] args) {
  34. /** Iinitialize the data */
  35. Person OLD = new Person("OLD", 2014, 9, 6);
  36. Person YOUNG = new Person("YOUNG", 1814, 9, 6);
  37. Person left = new Person("Left", 1814, 9, 6);
  38. Person right = new Person("Right", 2014, 9, 6);
  39. /** Input the number */
  40. int number, counter = -1;
  41. Scanner input = new Scanner(System.in);
  42. number = input.nextInt();
  43. /** Input the person information */
  44. Person[] person = new Person[10];
  45. for(int i = 0; i < number; i++) {
  46. person[i] = new Person();
  47. }
  48. String tempDate = new String();
  49. for(int i = 0; i < number; i++) {
  50. person[i].name = input.next();
  51. tempDate = input.next();
  52. formatInput(person[i], tempDate);
  53. // Fliter the invalid data
  54. if(person[i].Bigger(left) && person[i].Smaller(right)) {
  55. counter++;
  56. // Update OLD and YOUNG
  57. if(person[i].Smaller(OLD)) {
  58. OLD = person[i];
  59. }
  60. if(person[i].Bigger(YOUNG)) {
  61. YOUNG = person[i];
  62. }
  63. }
  64. }
  65. /** Display the result */
  66. if(counter == -1) {
  67. System.out.println("0");
  68. }
  69. else
  70. System.out.printf("%d %s %s", counter + 1, OLD.name, YOUNG.name);
  71. }
  72. public static void formatInput(Person temp, String tempDate) {
  73. // Cut the bits
  74. String date[] = tempDate.split("/");
  75. String year = date[0];
  76. String month = date[1];
  77. String day = date[2];
  78. // Convert String into integer
  79. int iYear = Integer.parseInt(year);
  80. int iMonth = Integer.parseInt(month);
  81. int iDay = Integer.parseInt(day);
  82. // Assign the integers into object
  83. temp.year = iYear;
  84. temp.month = iMonth;
  85. temp.day = iDay;
  86. }
  87. }

AC代码

  1. #include <iostream>
  2. using namespace std;
  3. typedef struct Person {
  4. char name[10];
  5. int year;
  6. int month;
  7. int day;
  8. }Person;
  9. Person OLD, YOUNG, leftEdge, rightEdge;
  10. void init();
  11. bool LessEqu(Person& a, Person& b);
  12. bool GreaterEqu(Person& a, Person& b);
  13. int main() {
  14. /* Initialize edge */
  15. init();
  16. /* Read data */
  17. int N, counter = 0;
  18. cin >> N;
  19. for(int i = 0; i < N; i++) {
  20. Person tmp;
  21. scanf("%s %d/%d/%d", &tmp.name, &tmp.year,
  22. &tmp.month, &tmp.day);
  23. /* Check tmp is valid or not? */
  24. if( GreaterEqu(tmp, leftEdge) && LessEqu(tmp, rightEdge) ) {
  25. counter++;
  26. if( LessEqu(tmp, OLD) )
  27. OLD = tmp;
  28. if( GreaterEqu(tmp, YOUNG) )
  29. YOUNG = tmp;
  30. }
  31. }
  32. if(counter == 0)
  33. printf("0\n");
  34. else
  35. printf("%d %s %s\n", counter, OLD.name, YOUNG.name);
  36. return 0;
  37. }
  38. void init() {
  39. YOUNG.year = leftEdge.year = 1814;
  40. OLD.year = rightEdge.year = 2014;
  41. YOUNG.month = OLD.month = 9;
  42. leftEdge.month = rightEdge.month = 9;
  43. YOUNG.day = OLD.day = 6;
  44. leftEdge.day = rightEdge.day = 6;
  45. }
  46. bool LessEqu(Person& a, Person& b) {
  47. if( a.year != b.year )
  48. return a.year <= b.year;
  49. else if( a.month != b.month )
  50. return a.month <= b.month;
  51. else
  52. return a.day <= b.day;
  53. }
  54. bool GreaterEqu(Person& a, Person& b) {
  55. if( a.year != b.year )
  56. return a.year >= b.year;
  57. else if( a.month != b.month )
  58. return a.month >= b.month;
  59. else
  60. return a.day >= b.day;
  61. }