14th蓝桥杯省赛JavaA组 填空题1:特殊日期
作者:
singleApo
,
2024-04-11 16:31:35
,
所有人可见
,
阅读 7
public class Main {
static int[] days = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public static boolean checkDate(int year, int month, int day) {
if (month == 0 || month > 12)
return false;
if (day == 0 || (month != 2 && day > days[month]))
return false;
if (month == 2) {
int leap = 0;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
leap = 1;
if (day > 28 + leap)
return false;
}
return true;
}
public static int getSum(int x) {
int sum = 0;
while (x > 0) {
sum += x % 10;
x /= 10;
}
return sum;
}
public static void main(String[] args) {
int ans = 0;
for (int i = 19000101; i <= 99991231; i++) {
int year = i / 10000;
int day = i % 100;
int month = i / 100 % 100;
if (checkDate(year, month, day)) {
if (getSum(year) == getSum(month) + getSum(day))
ans++;
}
}
System.out.print(ans);
}
}