提交时间:2026-03-01 20:15:49

运行 ID: 205527

#include <iostream> #include <vector> using namespace std; // 判断是否为闰年 bool isLeap(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } // 获取某年某月的天数 int getDaysInMonth(int year, int month) { if (month == 2) { return isLeap(year) ? 29 : 28; } // 4, 6, 9, 11月有30天 if (month == 4 || month == 6 || month == 9 || month == 11) { return 30; } // 其他月份31天 return 31; } int main() { int n; if (!(cin >> n)) return 0; // 统计数组,下标 0-6 分别代表 星期日, 星期一, ..., 星期六 // 初始化全为0 long long counts[7] = {0}; // 1900年1月1日是星期一 // 我们用 0=Sunday, 1=Monday, ..., 6=Saturday int currentWeekday = 1; // 计算1900年1月13日是星期几 // 1月1日是 weekday 1, 过12天是13日 currentWeekday = (currentWeekday + 12) % 7; // 遍历年份 for (int year = 1900; year < 1900 + n; ++year) { // 遍历月份 for (int month = 1; month <= 12; ++month) { // 记录当前月13号的星期 counts[currentWeekday]++; // 计算下个月13号的星期 // 下个月13号 = 当前月13号 + 当前月的天数 int daysInCurrentMonth = getDaysInMonth(year, month); currentWeekday = (currentWeekday + daysInCurrentMonth) % 7; } } // 输出顺序要求:星期六、星期日、星期一、星期二、星期三、星期四、星期五 // 对应下标:6, 0, 1, 2, 3, 4, 5 cout << counts[6]; for (int i = 0; i <= 5; ++i) { cout << " " << counts[i]; } cout << endl; return 0; }