1 solutions

  • 0
    @ 2026-11-15 13:06:39

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    
    bool isLeap(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    
        int y, w;
        cin >> y >> w;
    
        int monthDays[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (isLeap(y)) {
            monthDays[2] = 29;
        }
    
        // 当前月的 1 号是星期几(1~7)
        int curWeek = w;
    
        for (int m = 1; m <= 12; m++) {
            // 1. 输出星期表头
            cout << "MON TUE WED THU FRI SAT SUN\n";
    
            // 2. 输出 1 号之前的空格占位
            // 如果 1 号是星期一,curWeek=1,不需要空格;
            // 如果是星期二,curWeek=2,需要输出 1 个 "%3c" 的空格占位。
            for (int i = 1; i < curWeek; i++) {
                cout << "    ";
            }
    
            // 3. 依次输出日期
            int days = monthDays[m];
            for (int d = 1; d <= days; d++) {
                // 使用 %3d 格式化输出,不足 3 位补空格,实现个位与最后一字母对齐
                cout << setw(3) << d;
                
                // 如果今天是当前行的最后一天(周日)或者本月最后一天,换行
                if (curWeek == 7 || d == days) {
                    cout << "\n";
                    curWeek = 1; // 换行后星期重置为星期一
                } else {
                    curWeek++; // 否则星期数递增
                }
            }
    
            // 4. 月份之间输出空行(最后一个月后不输出)
            if (m != 12) {
                cout << "\n";
            }
        }
    
        return 0;
    }
    
    • 1

    Information

    ID
    201
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By