难点在“闰年”的定义。若非整百年份能被4整除则为闰年;整百年数当且仅当能被400整除时才是闰年。
只要写出每过一个月对应一年过了多少天,根据输入查对应的表即可。
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int year = in.nextInt(); int month = in.nextInt(); int day = in.nextInt(); // 判断闰年 boolean isLunarYear = year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0); // 节点表 int[] lunarYearDayCount = new int[] {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}; int[] normalYearDayCount = new int[13]; for (int i = 0; i <= 12; i++) { normalYearDayCount[i] = lunarYearDayCount[i]; } for (int i = 2; i <= 12; i++) { normalYearDayCount[i]--; } // 查表 if (isLunarYear) { System.out.println(lunarYearDayCount[month - 1] + day); } else { System.out.println(normalYearDayCount[month - 1] + day); } } } }