1 条题解

  • 0
    @ 2034-5-10 3:27:56

    练50.3 日记第几天

    解题思路

    用月份天数表,按闰年规则调整二月。答案为当前日d加上前m-1个月的天数。

    正确性说明

    按顺序完成所述运算后,每个输入元素都被处理在相应位置或统计项中。保存的中间状态与已处理数据对应,最终输出与题目定义一致。

    易错点

    不加入当前月整月天数;闰年影响仅二月。

    复杂度

    最多12个月,时间O(1),空间O(1)。

    参考代码(C++11)

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int y, m, d;
        cin >> y >> m >> d;
        int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0))
            days[2] = 29;
        int ans = d;
        for (int i = 1; i < m; i++)
            ans += days[i];
        cout << ans << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    340
    时间
    1000ms
    内存
    64MiB
    难度
    进阶
    标签
    递交数
    0
    已通过
    0
    上传者