题解 | #打印日期#

打印日期

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

#include <stdio.h>

int main() {
    int list1[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int list2[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int year, numday;
    while (scanf("%d %d", &year, &numday) != EOF) {
        int month = 0;
        int day;
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            for (int i = 0; i <= 12; i++) {
                day = numday;
                numday = numday - list2[i];
                if (numday > 0) {
                    month++;
                } else {
                    break;
                }
            }
        } else {
            for (int i = 0; i <= 12; i++) {
                day = numday;
                numday = numday - list1[i];
                if (numday > 0) {
                    month++;
                } else {
                    break;
                }
            }
        }
        printf("%4d-%02d-%02d", year, month, day);
    }
}

全部评论

相关推荐

挣K存W养DOG:他真的很中意你,为什么不回他
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务