返回题解分享
讨论 / 题解分享/ 帖子详情

顺子日期(结果填空) - 题解

#include <bits/stdc++.h>

using namespace std;

int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

bool isleap(int y)
{
    if (y % 400 == 0 || y % 4 == 0 && y % 100)
        return true;
    return false;
}

bool chk(string str)
{
    for (int i = 0; i + 2 < str.size(); ++i)
    {
        if (str[i] + 1 == str[i + 1] && str[i + 1] + 1 == str[i + 2])
            return true;
    }
    return false;
}

int main()
{
    int cury = 2022, curm = 1, curd = 1;
    if (isleap(cury))
        month[2] = 29;
    else
        month[2] = 28;

    int res = 0;
    while (cury == 2022)
    {
        char str[20];
        sprintf(str, "%d%02d%02d", cury, curm, curd); // 只可以接收char str[N]
        if(chk(str))
        {
            ++res;
            cout << str << endl;
        }
        string stringstr = str;//char str[N]强转为string
        ++curd;
        if (curd > month[curm])
        {
            curd = 1;
            ++curm;
        }
        if (curm > 12)
        {
            curm = 1;
            ++cury;
        }
    }
    cout << res << endl;

    return 0;
}
0 回复 0 转发 0 喜欢 1 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!