package interview.honor; import java.util.Scanner; public class First { public static void main(String[] args) { String inStr = new Scanner(System.in).nextLine(); String[] inArrs = inStr.split(" "); int yearStr = Integer.parseInt(inArrs[0]); int monthStr = Integer.parseInt(inArrs[1]); int ciWeek = Integer.parseInt(inArrs[2]); int ciDay = Integer.parseInt(inArrs[3]); // 第n年的第m月第一天是周几 int totalDays = 0; int dayNum = 0; int weekNum = 0; int[] PING_DAY = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int[] RUN_DAY = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 之前 for (int i = 2000; i < yearStr; i++) { if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) { // System.out.println(year + "是闰年"); totalDays += 366; } else { // System.out.println(year + "不是闰年"); totalDays += 365; } } // 当年 if (monthStr == 2) { totalDays += 31; if (yearStr % 4 == 0 && yearStr % 100 != 0 || yearStr % 400 == 0) { // 闰年 // 一号是周几 weekNum = (totalDays + 6) % 7; //System.out.println("weekNum:"+weekNum); if ((ciWeek - 1)*7 + ciDay < weekNum || (ciWeek - 1)*7 + ciDay > 29) { System.out.println(0); return; } } else { // 平年 weekNum = (totalDays + 6) % 7; //System.out.println("weekNum:"+weekNum); if ((ciWeek - 1)*7 + ciDay < weekNum || (ciWeek - 1)*7 + ciDay > 28) { System.out.println(0); return; } } dayNum = (7 - weekNum + 1) + (ciWeek - 2) * 7 + ciDay; } else { if (yearStr % 4 == 0 && yearStr % 100 != 0 || yearStr % 400 == 0) { // 闰年 for (int i = 0; i < monthStr - 1; i++) { totalDays += RUN_DAY[i]; } } else { // 平年 for (int i = 0; i < monthStr - 1; i++) { totalDays += PING_DAY[i]; } } weekNum = (totalDays + 6) % 7; //System.out.println("weekNum:"+weekNum); if ((ciWeek - 1)*7 + ciDay < weekNum || (ciWeek - 1)*7 + ciDay > PING_DAY[monthStr]) { System.out.println(0); return; } } //System.out.println("WeekNum" + weekNum); dayNum = (7 - weekNum + 1) + (ciWeek - 2) * 7 + ciDay; String result = yearStr + "-" + monthStr + "-" + dayNum; System.out.println(result); } }
2021-04-25
最新推荐文章于 2022-01-27 13:12:47 发布