题解 | #日期累加#

日期累加

https://www.nowcoder.com/practice/eebb2983b7bf40408a1360efb33f9e5d

#include <cstdio>
int main() {
    int m;
    int mdays[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int year, month, day, after;
    while (scanf("%d", &m) != EOF) {
        for (int i = 0; i < m; ++i) {
            scanf("%d%d%d%d", &year, &month, &day, &after);
            while (after--) {
                bool isLeap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
                if (isLeap) {
                    mdays[2] = 29;
                } else {
                    mdays[2] = 28;
                }
                day++;
                if (day > mdays[month]) {
                    month++;
                    day = 1;
                    if (month > 12) {
                        year++;
                        month = 1;
                    }
                }
            }
            printf("%04d-%02d-%02d\n", year, month, day);
        }
    }
}

全部评论

相关推荐

10-11 17:45
门头沟学院 Java
走吗:别怕 我以前也是这么认为 虽然一面就挂 但是颇有收获!
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
2
1
分享
牛客网
牛客企业服务