题目描述
小蓝正在上小学,老师要求同学们在暑假每天记日记。可是小蓝整个暑假都在玩,直到最后一天才想起要记日记。于是小蓝赶紧编了一些日记交给老师。
没想到,日记很快就被老师发现了问题,原来小蓝记完 8 月 31 日的日记,竟又记了 8 月 32 日和 8 月 33 日的日记。这显然是有问题的,因为根本没有 8 月 32 日和 8 月 33 日。
给定一个月份和一个日期,请问 2021 年有没有这一天。
输入描述
输入的第一行包含一个整数 m,表示月份。
第二行包含一个整数 d,表示日期。
其中,1≤m≤20,1≤d≤40。
输出描述
如果2021年有 m 月 d 日,输入 yes,否则输出 no。
输入输出样例
示例 1
输入
8
32
输出
no
示例 2
输入
2
28
输出
yes
运行限制
最大运行时间:1s
最大运行内存: 128M
import java.util.Scanner;
public class 合法日期 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int month = scanner.nextInt();
int day = scanner.nextInt();
//(month的数据范围是0-20)
//月份大于12肯定不合法直接return,防止数组下标索引越界
if (month > 12) {
System.out.println("no");
return;
}
if (f(month, day)) {
System.out.println("yes");
} else {
System.out.println("no");
}
}
private static boolean f(int month, int day) {
int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (days[month] < day) {
return false;
}
return true;
}
}