某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。
这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过 200 岁的老人,而今天是 2014 年 9 月 6 日,所以超过 200 岁的生日和未出生的生日都是不合理的,应该被过滤掉。
输入格式:
输入在第一行给出正整数 N,取值在(0,10];随后 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
思路
这题面向对象思想浓厚,第一次用了Java来写
第4个测试点没过。。。。
第二次回顾用C++写,代码AC。
代码
import java.util.Scanner;
class Person {
public String name;
public int year;
public int month;
public int day;
public Person() {
}
public Person(String name, int year, int month, int day) {
this.name = name;
this.year = year;
this.month = month;
this.day = day;
}
public boolean Bigger(Person left) {
if(this.year != left.year)
return this.year >= left.year;
else if(this.month != left.month)
return this.month >= left.month;
else
return this.day >= left.day;
}
public boolean Smaller(Person right) {
if(this.year != right.year)
return this.year <= right.year;
else if(this.month != right.month)
return this.month <= right.month;
else
return this.day <= right.day;
}
}
public class Main {
public static void main(String[] args) {
/** Iinitialize the data */
Person OLD = new Person("OLD", 2014, 9, 6);
Person YOUNG = new Person("YOUNG", 1814, 9, 6);
Person left = new Person("Left", 1814, 9, 6);
Person right = new Person("Right", 2014, 9, 6);
/** Input the number */
int number, counter = -1;
Scanner input = new Scanner(System.in);
number = input.nextInt();
/** Input the person information */
Person[] person = new Person[10];
for(int i = 0; i < number; i++) {
person[i] = new Person();
}
String tempDate = new String();
for(int i = 0; i < number; i++) {
person[i].name = input.next();
tempDate = input.next();
formatInput(person[i], tempDate);
// Fliter the invalid data
if(person[i].Bigger(left) && person[i].Smaller(right)) {
counter++;
// Update OLD and YOUNG
if(person[i].Smaller(OLD)) {
OLD = person[i];
}
if(person[i].Bigger(YOUNG)) {
YOUNG = person[i];
}
}
}
/** Display the result */
if(counter == -1) {
System.out.println("0");
}
else
System.out.printf("%d %s %s", counter + 1, OLD.name, YOUNG.name);
}
public static void formatInput(Person temp, String tempDate) {
// Cut the bits
String date[] = tempDate.split("/");
String year = date[0];
String month = date[1];
String day = date[2];
// Convert String into integer
int iYear = Integer.parseInt(year);
int iMonth = Integer.parseInt(month);
int iDay = Integer.parseInt(day);
// Assign the integers into object
temp.year = iYear;
temp.month = iMonth;
temp.day = iDay;
}
}
AC代码
#include <iostream>
using namespace std;
typedef struct Person {
char name[10];
int year;
int month;
int day;
}Person;
Person OLD, YOUNG, leftEdge, rightEdge;
void init();
bool LessEqu(Person& a, Person& b);
bool GreaterEqu(Person& a, Person& b);
int main() {
/* Initialize edge */
init();
/* Read data */
int N, counter = 0;
cin >> N;
for(int i = 0; i < N; i++) {
Person tmp;
scanf("%s %d/%d/%d", &tmp.name, &tmp.year,
&tmp.month, &tmp.day);
/* Check tmp is valid or not? */
if( GreaterEqu(tmp, leftEdge) && LessEqu(tmp, rightEdge) ) {
counter++;
if( LessEqu(tmp, OLD) )
OLD = tmp;
if( GreaterEqu(tmp, YOUNG) )
YOUNG = tmp;
}
}
if(counter == 0)
printf("0\n");
else
printf("%d %s %s\n", counter, OLD.name, YOUNG.name);
return 0;
}
void init() {
YOUNG.year = leftEdge.year = 1814;
OLD.year = rightEdge.year = 2014;
YOUNG.month = OLD.month = 9;
leftEdge.month = rightEdge.month = 9;
YOUNG.day = OLD.day = 6;
leftEdge.day = rightEdge.day = 6;
}
bool LessEqu(Person& a, Person& b) {
if( a.year != b.year )
return a.year <= b.year;
else if( a.month != b.month )
return a.month <= b.month;
else
return a.day <= b.day;
}
bool GreaterEqu(Person& a, Person& b) {
if( a.year != b.year )
return a.year >= b.year;
else if( a.month != b.month )
return a.month >= b.month;
else
return a.day >= b.day;
}