Run ID:132577

提交时间:2025-10-10 08:46:10

#include <iostream> #include <vector> using namespace std; // 判断是否为闰年 bool isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } // 获取某年某月的天数 int getDaysInMonth(int year, int month) { if (month == 2) { return isLeapYear(year) ? 29 : 28; } vector<int> daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; return daysInMonth[month - 1]; } // 计算从基准日期到目标日期的天数差 int calculateDaysDifference(int targetYear, int targetMonth, int targetDay) { // 基准日期:2021年3月18日 int baseYear = 2021, baseMonth = 3, baseDay = 18; // 如果目标日期在基准日期之前,交换计算 bool swap = false; if (targetYear < baseYear || (targetYear == baseYear && targetMonth < baseMonth) || (targetYear == baseYear && targetMonth == baseMonth && targetDay < baseDay)) { swap = true; swap(targetYear, baseYear); swap(targetMonth, baseMonth); swap(targetDay, baseDay); } int days = 0; // 计算基准日期到年底的天数 for (int month = baseMonth; month <= 12; month++) { int daysInMonth = getDaysInMonth(baseYear, month); if (month == baseMonth) { days += daysInMonth - baseDay; } else { days += daysInMonth; } } // 计算中间年份的天数 for (int year = baseYear + 1; year < targetYear; year++) { days += isLeapYear(year) ? 366 : 365; } // 计算目标年份到目标日期的天数 for (int month = 1; month < targetMonth; month++) { days += getDaysInMonth(targetYear, month); } days += targetDay; // 如果交换过,取负值 if (swap) { days = -days; } return days; } // 计算星期几(0=周日, 1=周一, ..., 6=周六) int calculateWeekday(int year, int month, int day) { // 2021年3月18日是星期四(对应数字4) int baseDays = calculateDaysDifference(2021, 3, 18); int targetDays = calculateDaysDifference(year, month, day); int daysDiff = targetDays - baseDays; // 计算星期几(基准日是星期四=4) int weekday = (4 + daysDiff % 7 + 7) % 7; // 调整:0=周日, 1=周一, ..., 6=周六 if (weekday == 0) weekday = 7; // 按照题目要求,周日应该是7 return weekday; } int main() { int year, month, day; cin >> year >> month >> day; // 计算天数差 int daysDifference = calculateDaysDifference(year, month, day); // 计算星期几 int weekday = calculateWeekday(year, month, day); // 输出结果 cout << daysDifference << endl; cout << "*" << weekday << endl; return 0; }