题解 | #打印日期#

打印日期

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

#include<cstdio>

int main() {
    int y, m, d, n;
    int flag;
    int sum;
    int days[2][12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
                       31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
                      };
    while (scanf("%d%d", &y, &n) != EOF) {
        if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)flag = 1;
        else flag = 0;
        sum = 0;
        m = 0;
        while (sum + days[flag][m] < n) {
            sum += days[flag][m];
            m++;
        }
        m++;
        d = n - sum;
        printf("%04d-%02d-%02d\n", y, m, d);
    }
    return 0;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务