题目
定义表示日期的结构体 Date(包含年、月、日),从标准输入读入年月日,综合考虑大小月与闰年二月天数,判断该日期是否为合法日历日期。
输入格式:一行输入三个正整数,分别表示年、月、日。
输出格式:若为合法日期输出 yes,否则输出 no,末尾换行。
数据范围与约定:1 ≤ 年 ≤ 9999。
思路与算法
1. 多维约束校验
① 检查月份是否在 1~12 之间;
② 根据年份判定闰年,确定 2 月最大天数(28 或 29);
③ 检查日是否在 1 到对应月份的最大天数之间。
2. 结构体组织逻辑
将日期字段封装进结构体,通过独立函数 int is_valid_date(struct Date d) 返回布尔状态。
完整程序
import java.util.Scanner;
class DateValidator {
public static boolean isLeapYear(int y) {
return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
}
public static boolean isValid(int y, int m, int d) {
if (y < 1 || y > 9999 || m < 1 || m > 12 || d < 1) {
return false;
}
int[] days = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeapYear(y)) {
days[2] = 29;
}
return d <= days[m];
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int y = sc.nextInt();
int m = sc.nextInt();
int d = sc.nextInt();
System.out.println(DateValidator.isValid(y, m, d) ? "yes" : "no");
}
}运行示例
输入:
2024 2 29输出:
yes