1 solutions

  • 0
    @ 2025-11-27 11:58:16

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    
    // 每个月的天数(默认平年)
    int days_in_month[13] = {0,
        31, 28, 31, 30, 31, 30,
        31, 31, 30, 31, 30, 31
    };
    
    // 星期几的名字
    string week[7] = {"MON","TUE","WED","THU","FRI","SAT","SUN"};
    
    // 判断闰年
    bool isLeap(int year) {
        return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
    }
    
    int main() {
        int m;
        cin >> m;
    
        int year = 2025;
    
        // 如果闰年,2月变成29天
        if (isLeap(year)) days_in_month[2] = 29;
    
        // 2025/1/1 是星期三 (WED)
        int first_day = 2; // 0=MON,1=TUE,2=WED,...
    
        // 累加前几个月的天数,得到m月1日的星期
        int offset = 0;
        for (int i = 1; i < m; i++) offset += days_in_month[i];
        int start_day = (first_day + offset) % 7;
    
        // 打印表头
        for (int i = 0; i < 7; i++) {
            cout << week[i];
            if (i < 6) cout << " ";
        }
        cout << "\n";
    
        // 打印空格(对齐)
        for (int i = 0; i < start_day; i++) cout << "    ";
    
        int days = days_in_month[m];
        for (int d = 1; d <= days; d++) {
           
            
            if(d < 10) cout << "  "; 
    		else if(d <= 31) cout << " ";
    		cout << d;
    		if((start_day + d) % 7 == 0) cout << "\n";
    		else cout << " ";
    		
        }
        cout << "\n";
    
        return 0;
    }
    
    
    
    • 1

    Information

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