题解 | #日期类#

日期类

https://www.nowcoder.com/practice/130aa2d7d1f5436b920229dca253893b

加1之后处理一下细节就完了

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;

int daytab[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}};

bool isLeapYear(int year)
{
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

int main()
{
    int i, j, number1 = 0, number2 = 0, row, n, year, month, day;
    string str;
    cin >> n;
    while (n--)
    {
        cin >> str;
        year = stoi(str);
        cin >> str;
        month = stoi(str);
        cin >> str;
        day = stoi(str);
        // cout << year << "hdu" << month << "hdu" << day << endl;
        int row = isLeapYear(year);
        day++;
        if (day > daytab[row][month])
        {
            day = 1;
            month++;
            if (month > 12)
            {
                month = 1;
                year++;
            }
        }
        cout << year << '-';
        if (month < 10)
            cout << '0' << month << '-';
        else
            cout << month << '-';
        if (day < 10)
            cout << '0' << day;
        else
            cout << day;
        cout << endl;
    }

    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务