题解 | #日期累加#

日期累加

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

#include <bits/stdc++.h>
using namespace std;
int mouth_day[2][13] {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
int runnian(int year) {
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)return 1;
    else return 0;
}
int nian_day(int year) {
    if (runnian(year))return 366;
    else return 365;
}
int main() {
    int m = 0, year = 0, mouth = 0, day = 0, after = 0, j = 0;
    scanf("%d", &m);
    while (m != 0) {
        scanf("%d %d %d %d", &year, &mouth, &day, &after);
        int row = runnian(year);
        for (j = 0; j < mouth; j++) {
            after = after + mouth_day[row][j];
        }
        after = after + day;
        while (after > nian_day(year)) {
            after -= nian_day(year);
            year++;
        }
        mouth = 0;
        row = runnian(year);
        while (after > mouth_day[row][mouth]) {
            after -= mouth_day[row][mouth];
            mouth++;
        }
        day = after;
        printf("%04d-%02d-%02d\n", year, mouth, day);
        m--;
    }
    return 0;
}

全部评论

相关推荐

程序员小白条:你是沟通了900个,不是投了900份简历,你能投900份,意味着对面都要回复你900次,你早就找到实习了,没亮点就是这样的,别局限地区,时间投的也要早,现在都要7月了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务